title: Write struct to binary file, then read file and print data.
body: <p>I'm self-studying C and trying to understand memory allocation and basic I/O. My code creates a "database" object which contains a pointer to an "address" struct. This last pointer I use as an array, and allocate for it the required space. I write the data to a file, and the I open the file to read back the data. Valgrind shows no memory issues.</p>
  
  <p>However, when built and ran on Windows 7 (built with MingW gcc 4.8.1), it dies when it tries to read the file. Furthermore, under win 7, If I give a MAX_ROWS value of 26, the program enters an infinite loop.</p>
  
  <p>Furthermore, I'm not sure about the efficiency/correctness of this piece of code. </p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define MAX_ROWS 1000 * 1000
  #define MAX_DATA 512
  
  typedef struct Address {
      int id;
      int set;
      char *name;
      char *email;
  } Address;
  
  typedef struct Database {
      int max_rows;
      int max_data;
      Address *rows;
  } Database;
  
  
  int main(int argc, char *argv[]) 
  {       
      Database *db = malloc(sizeof(Database));
      if(db == NULL)  { 
          printf("Could not allocate mem for db");
          free(db);
          exit(1);
      }
  
      db-&gt;max_data = MAX_DATA;
      db-&gt;max_rows = MAX_ROWS;
  
      db-&gt;rows = malloc(sizeof(Address) * db-&gt;max_rows);
      if(db-&gt;rows == NULL) printf("Could not allocate mem for db-&gt;rows");
  
      // Create static data 
      for(int i = 0; i &lt; MAX_ROWS; i++) {                
          Address addr = { .id = i, .name = "Jack Sparrow", .email = "jacksp@example.com"};
          // Assign it
          *(db-&gt;rows + i) = addr;
      }
  
      // Open a file to write the data
      FILE *f = fopen("temp.dat", "w+");
      if(!f) printf("Could not open file");
  
      // The reason I cannot write the struct in one move
      // is because it has been dynamically sized
      int rc = fwrite(&amp;db-&gt;max_rows, sizeof(int), 1, f); 
      rc = fwrite(&amp;db-&gt;max_data, sizeof(int), 1, f); 
      rc = fwrite(db-&gt;rows, sizeof(Address) * db-&gt;max_rows, 1, f);     
      if(!rc) printf("could not write db");
  
      fclose(f);
      free(db-&gt;rows);
      free(db);
  
      // Now let's read the file
      f = fopen("temp.dat", "r");
      if(!f) printf("Could not open file\n");
  
      // Create a new Database pointer to store file data
      Database *tmpdb = malloc(sizeof(Database));
      if(!tmpdb) printf("could not allocate memory to tmpdb\n");
      rc = fread(&amp;tmpdb-&gt;max_rows, sizeof(int) , 1, f); 
  
      if(!rc) printf("could not read max_rows\n");
      rc = fread(&amp;tmpdb-&gt;max_data, sizeof(int) , 1, f); 
      if(!rc) printf("could not read max_data\n");
  
      printf("%d\n", tmpdb-&gt;max_rows);
  
      tmpdb-&gt;rows = malloc(sizeof(Address) * tmpdb-&gt;max_rows);
      if(!tmpdb-&gt;rows) printf("could not allocate rows\n");
  
      // This dies on windows (MingW gcc), but runs fine on ubuntu!  
      rc = fread(tmpdb-&gt;rows, sizeof(Address) * tmpdb-&gt;max_rows , 1, f); 
      if(!rc) printf("could not read db\n");
  
      fclose(f);
      free(tmpdb-&gt;rows);
      free(tmpdb);
  
      return 0;
  }
  </code></pre>