0

I have a code that contains some functions unrelated to database. when I added functions for connecting to database, the code occurs Segmentation fault (core dumped). but when I use those functions (connecting to db) in an isolated file there is no problem.

#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>

void exit_db(PGconn *connection){
        //this function close the connection
        //between progrem and database.
        PQfinish(connection);
}

void make_connection(PGconn *connection){
        //this function stablish a connection 
        //between program and database.
        connection = PQconnectdb("user=user "\
                                "password=123 "\
                                "dbname=project_db");
        if(PQstatus(connection) == CONNECTION_BAD){
                printf("%s\n", PQerrorMessage(connection));
                exit_db(connection);
        }
}

void create_table_fp_stores_data(PGresult *re, PGconn *connection){
        //this function create fp_stores_data table if does not exist.
        re = PQexec(connection ,"CREATE TABLE IF NOT EXISTS fp_stores_data_test (time TIME,"\
                                                        "province VARCHAR(20), city VARCHAR(20),"\
                                                        "market_id INTEGER );");
                if(PQresultStatus(re)==PGRES_COMMAND_OK){
                        printf("table created!\n");
                }else{
                        printf("%s\n", PQresultErrorMessage(re));
                        printf("%s\n", PQerrorMessage(connection));
                }
        PQclear(re);
}

int main(){
    PGconn *con;
    PGresult *res;
    make_connection(con);
    create_table_fp_stores_data(res, con);
    return 0;
}

this code has no problem but when I add some unrelated functions to this, it occurs problem.I can put my whole code here but I'm trying to avoid congestion.

Amir reza Riahi
  • 1,540
  • 2
  • 8
  • 34
  • 2
    Does this answer your question? [How do I modify a pointer that has been passed into a function in C?](https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c), if you want to modify passed `connection` inside `make_connection` you have to pass pointer to pointer `**`. – rafix07 Jul 06 '20 at 10:40
  • @LaurenzAlbe can I use `malloc` for this part of code (database) ? does it help? – Amir reza Riahi Jul 06 '20 at 10:43
  • @rafix07 you mean change `void make_connection(PGconn *connection)` to `void **make_connection(PGconn *connection)`? – Amir reza Riahi Jul 06 '20 at 10:46

1 Answers1

2

The problem is in the definition of

void make_connection(PGconn *connection)

The PGconn * is passed by value, so connection inside the function body is a different variable than con in your main function.

You assign to connection in make_connection, but that doesn't change the value of con.

There are two solutions:

  1. Pass the address of con:

     void make_connection(PGconn **connection) {
         *connection = PQconnectdb(...);
     }
    
     make_connection(&con);
    
  2. (better) have the function return the pointer:

     PGconn *make_connection() {
         PGconn *result;
         result = PQconnectdb(...);
         return result;
     }
    
     con = make_connection();
    
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263