Skip to main content
edited tags
Link
timrau
  • 23.1k
  • 4
  • 55
  • 67
added 929 characters in body
Source Link
Ajit
  • 675
  • 2
  • 15
  • 27

This is a problem from learn C the hardway. This is database management system in C I have three structures :-

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

 struct Database {
int rows;
    struct Address *row;
 };

 struct Connection {
    FILE *file;
    struct Database *db;
 };

I trying to initialize the database structure. However I am getting a segfault

    void Database_create(struct Connection *conn, int no_of_rows)
    {
      int i = 0;
  conn->db->row_num = no_of_rows; 

      for(i = 0; i < conn->db->row_num; i++) {

      // make a prototype to initialize it
      struct Address addr;
      addr.id = i;
      addr.set = 0;

      // then just assign it
      conn->db->rows[i] = addr;
      }
  }

I've made another function which allocates memory to these structures.

     struct Connection *Database_open(const char *filename, char mode)
      {   
        struct Connection *conn = malloc(sizeof(struct Connection));
        if(!conn) die("Memory error");

    int number = conn->db->rows;

        conn->db = malloc(sizeof(struct Database));
    if(!conn->db) die("Memory error");

        conn->db->row = malloc(sizeof(*conn->db->row) * number);
        if(!conn->db->row) die("Memory error");

        if(mode == 'c') {
        conn->file = fopen(filename, "w");
        } else {
         conn->file = fopen(filename, "r+");

      if(conn->file) {
         Database_load(conn);
      }
   }

   if(!conn->file) die("Failed to open the file");

      return conn;
   }

valgrind says "Use of uninitialized value of size 4" in Database_open()

Could anyone suggest what I might be doing wrong here?

This is a problem from learn C the hardway. This is database management system in C I have three structures :-

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

 struct Database {
int rows;
    struct Address *row;
 };

 struct Connection {
    FILE *file;
    struct Database *db;
 };

I trying to initialize the database structure. However I am getting a segfault

 void Database_create(struct Connection *conn, int no_of_rows)
 {
      int i = 0;
  conn->db->row_num = no_of_rows; 

      for(i = 0; i < conn->db->row_num; i++) {

      // make a prototype to initialize it
      struct Address addr;
      addr.id = i;
      addr.set = 0;

      // then just assign it
      conn->db->rows[i] = addr;
      }
  }

Could anyone suggest what I might be doing wrong here?

This is a problem from learn C the hardway. This is database management system in C I have three structures :-

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

 struct Database {
int rows;
    struct Address *row;
 };

 struct Connection {
    FILE *file;
    struct Database *db;
 };

I trying to initialize the database structure. However I am getting a segfault

    void Database_create(struct Connection *conn, int no_of_rows)
    {
      int i = 0;
  conn->db->row_num = no_of_rows; 

      for(i = 0; i < conn->db->row_num; i++) {

      // make a prototype to initialize it
      struct Address addr;
      addr.id = i;
      addr.set = 0;

      // then just assign it
      conn->db->rows[i] = addr;
      }
  }

I've made another function which allocates memory to these structures.

     struct Connection *Database_open(const char *filename, char mode)
      {   
        struct Connection *conn = malloc(sizeof(struct Connection));
        if(!conn) die("Memory error");

    int number = conn->db->rows;

        conn->db = malloc(sizeof(struct Database));
    if(!conn->db) die("Memory error");

        conn->db->row = malloc(sizeof(*conn->db->row) * number);
        if(!conn->db->row) die("Memory error");

        if(mode == 'c') {
        conn->file = fopen(filename, "w");
        } else {
         conn->file = fopen(filename, "r+");

      if(conn->file) {
         Database_load(conn);
      }
   }

   if(!conn->file) die("Failed to open the file");

      return conn;
   }

valgrind says "Use of uninitialized value of size 4" in Database_open()

Could anyone suggest what I might be doing wrong here?

Source Link
Ajit
  • 675
  • 2
  • 15
  • 27

Initializing a nested structure

This is a problem from learn C the hardway. This is database management system in C I have three structures :-

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

 struct Database {
int rows;
    struct Address *row;
 };

 struct Connection {
    FILE *file;
    struct Database *db;
 };

I trying to initialize the database structure. However I am getting a segfault

 void Database_create(struct Connection *conn, int no_of_rows)
 {
      int i = 0;
  conn->db->row_num = no_of_rows; 

      for(i = 0; i < conn->db->row_num; i++) {

      // make a prototype to initialize it
      struct Address addr;
      addr.id = i;
      addr.set = 0;

      // then just assign it
      conn->db->rows[i] = addr;
      }
  }

Could anyone suggest what I might be doing wrong here?