Pointer to one structure to another, writing and reading from file gives SegFault

I am clearing my C skills from Learn C The Hard Way, I am currently in the 17th Exercise .

I am doing the Extra Credits part. Generating the database code listed on this page to get I am trying

"Change the code to accept the parameters for MAX_DATA and MAX_ROWS, store them in the database structure and write this to a file, thus creating a database that can be arbitrarily defined."

So, I commented out the #define directives and changed the address and database structures as stated:

struct Address {
    int id;
    int set;
    char *name;
    char *email;
};

struct Database {
    int MAX_DATA;
    int MAX_ROWS;
    struct Address *rows;
};

      

This way I can get the MAX_DATA and MAX_ROWS parameters from the user to create a database of his liking. Other things that I changed in the code -

Database_create function:

void Database_create(struct Connection *conn, int MAX_DATA, int MAX_ROWS)
{
        int i = 0;
        conn->db->MAX_DATA = MAX_DATA;
        conn->db->MAX_ROWS = MAX_ROWS;
        conn->db->rows = malloc(sizeof(struct Address) * MAX_ROWS);
        for(i = 0; i < MAX_ROWS; i++) {
                struct Address addr = {.id = i, .set = 0};
                conn->db->rows[i] = addr;
        }
}

      

The part where I get "Invalid Read 4 Bytes" and SegFault:

void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
    struct Address *addr = &conn->db->rows[id];
    int MAX_DATA = conn->db->MAX_DATA;
    if(addr->set) die("Already set, delete it first");
    /* This if statement gives error for addr->set */
    addr->set = 1;
    addr->name = malloc(sizeof(char) * MAX_DATA);
    addr->email = malloc(sizeof(char) * MAX_DATA);
    // WARNING: bug, read the "How To Break It" and fix this
    char *res = strncpy(addr->name, name, MAX_DATA);
    // demonstrate the strncpy bug
    if(!res) die("Name copy failed");

    res = strncpy(addr->email, email, MAX_DATA);
    if(!res) die("Email copy failed");
}

      

I know this piece of code is not a whole piece, but I cannot paste all the code here. So, I posted it here: http://pastebin.com/EbKShT3r I can create and write a database for the first run using the "c" option. However, adding entries using the "s" option results in a segmentation fault.

EDIT : So finally I got this program working with the solution given by @WhizCraig However, to free up memory, here's what I'm trying:

void Database_close(struct Connection *conn)
{
    int i;
    if (conn) {
        int MAX_ROWS = conn->db->MAX_ROWS;
        for (i=0; i<MAX_ROWS; i++) {
            struct Address *row = conn->db->rows+i;
            if (row->set) {
                free(row->name);
                free(row->email);
            }
        }
        free(conn->db->rows);
        if(conn->file) fclose(conn->file);
        if(conn->db) free(conn->db);
        free(conn);
    }
}

      

And, I get errors in Valgrind, there is a memory leak. I don't understand the error in the above code. However, the main problem seems to be solved :)

+3


source to share


2 answers


struct Address {
    int id;
    int set;
    char *name;
    char *email;
};

struct Database {
    int MAX_DATA;
    int MAX_ROWS;
    Address **rows; // USE ARRAY OF POINTERS
};

void Database_create(Connection *conn, int MAX_DATA, int MAX_ROWS)
{
        int i = 0;
        conn->db->MAX_DATA = MAX_DATA;
        conn->db->MAX_ROWS = MAX_ROWS;
        conn->db->rows = (Address**)malloc(sizeof(Address*) * MAX_ROWS);

        for(i = 0; i < MAX_ROWS; i++) {
                conn->db->rows[i] = (Address*)malloc(sizeof(Address));
                conn->db->rows[i]->id = i;
                conn->db->rows[i]->set = 0;
        }
}

void Database_set(Connection *conn, int id, const char *name, const char *email)
{
    if (!(conn && conn->db && conn->db->rows && conn->db->rows[id])) return;

    Address *addr = conn->db->rows[id];
    int MAX_DATA = conn->db->MAX_DATA;

    if(addr->set == 0) die("Already set, delete it first");

    addr->set = 1;
    addr->name = malloc(sizeof(char) * MAX_DATA);
    addr->email = malloc(sizeof(char) * MAX_DATA);

    char *res = strncpy(addr->name, name, MAX_DATA);
    if(!res) die("Name copy failed");

    res = strncpy(addr->email, email, MAX_DATA);
    if(!res) die("Email copy failed");
}

void Database_close(Connection *conn)
{
        size_t i;
        if(conn) {
                if (con->db && conn->db->rows) {
                        for (i = 0; i < conn->db->MAX_ROWS; i++) {

                                Address *cur = conn->db->rows[i];
                                free(cur);

                        }
                }

                if(conn->file) fclose(conn->file);
                if(conn->db) free(conn->db);
                free(conn);
        }
}

      



0


source


I modified the Rohit code posted here: http://pastebin.com/MvLXkDCz to fix the memory leaks. It goes through Valgrind without any error. The following are the relevant changes:

void Database_close(Connection *conn)                                                 
{                                                                                     
  size_t i;                                                                           
  if(conn) {                                                                          
    if(conn->db && conn->db->rows) {                                                  
      for(i = 0; i < conn->db->max_rows; i++) {                                       
        Address *cur = conn->db->rows[i];                                             
        free(cur->name);                                                              
        free(cur->email);                                                             
        free(cur);                                                                    
      }                                                                               
      free(conn->db->rows);                                                           
    }                                                                                 
    if(conn->file) fclose(conn->file);                                                
    if(conn->db) free(conn->db);                                                      
    free(conn);                                                                       
  }                                                                                   
}   

void Database_delete(Connection *conn, int id)                                        
{                                                                                     
  conn->db->rows[id]->set = 0;                                                        
} 

      



You can find the complete program here: https://github.com/sookoor/Learn-C-the-Hard-Way/blob/master/ex17.c

+1


source







All Articles