title: Why does realloc() crash my program?
body: <p>I am trying to resize an array dynamically through the use of <code>realloc</code>. This array is initialized outside of my function using <code>malloc</code>.</p>
  
  <p>Here's my function:</p>
  
  <pre><code>size_t verarbeite_anlagendatei(ANLAGE *anlage_arr) {    
      FILE *fp;
      ANLAGE anlage;
  
      fp = fopen("anlagen.dat", "r");
  
      if(fp == NULL) {
          printf("Anlagedatei existiert nicht. Bitte mit Menuepunkt (0) weiter machen.\n");
          return 0;
      }
  
      int index = 0;
      size_t size = 1;
  
      while(fscanf(fp, "%d %s %s %f %d %d",   
                  &amp;anlage.InventarNr, 
                  anlage.Anlagenbez, 
                  anlage.Standort, 
                  &amp;anlage.Basiswert, 
                  &amp;anlage.Nutzdauer, 
                  &amp;anlage.AnschJahr) != EOF) {
          if(index &gt; 0) {
              size++;
              realloc(anlage_arr, size * sizeof(ANLAGE));
          }
          anlage_arr[index] = anlage;
          index++;
      }
      return size;    
  }
  </code></pre>
  
  <p>I know that I have to initialize a new pointer to the <code>ANLAGE</code> type and check if it's <code>NULL</code> after my call to <code>realloc</code>, but since this function always crashes the program, I've skipped it in this case.</p>