title: malloc() struct in a function, free() before ending program
body: <p>I am learning the basics of C, and now working with malloc(). Say I have a function that asks the user for input, populates a structure with said data, and that structure in saved in an array (all passed by reference from a main function).</p>
  
  <p>I ran the program through Valgrind, I get the "still reachable" bytes (which should be ok right? not considered a leak), but any data saved I get lost blocks.</p>
  
  <p>How would I go freeing that memory after the program has finished running? Also I have some (2) questions within the code just to clarify some things and would appreciate if someone could explain them to me.</p>
  
  <p>Here is some code similar to what I am trying to do:</p>
  
  <p>I have the following struct declared: </p>
  
  <pre><code>struct Person {
    char name[MAX_INPUT];
    int age;
  };
  </code></pre>
  
  <p>I am writing a function that goes like this:</p>
  
  <pre><code>int function2(struct Person *list, int *index) {
    struct Person *prsn = malloc(sizeof(struct Person)); 
    // !Why do we sometimes cast the malloc or not? 
    // I sometimes get errors when I do, sometimes when I don't, 
    // while the surrounding code is pretty much the same.
    assert(prsn != NULL);
  
    // User input code goes here ... 
  
    // Now to save the Person created
    strcpy(prsn-&gt;name, nameInput);
    prsn-&gt;age = ageInput;
    list[(*index)++] = *prsn; 
    // !Why use the dereferencing *prsn here? 
    // why not directly prsn? Or is that saving the memory address and not very useful.
  
    return 0;
  }
  </code></pre>
  
  <p>And this is my main function:</p>
  
  <pre><code>int main(int argc, char *argv[]) { 
    struct Person personList[MAX_SIZE];
    int index;
  
    function2(personList, &amp;index);
  
    // Before closing, I want to free any mallocs I have done here. free()
  
    return 0;
  }
  </code></pre>
  
  <p>Valgrind report:</p>
  
  <pre><code>LEAK SUMMARY:
  ==1766==    definitely lost: 44 bytes in 1 blocks
  ==1766==    indirectly lost: 0 bytes in 0 blocks
  ==1766==      possibly lost: 0 bytes in 0 blocks
  ==1766==    still reachable: 10,355 bytes in 34 blocks
  ==1766==         suppressed: 0 bytes in 0 blocks
  </code></pre>
  
  <p>Thank you in advance.</p>
  
  <p>Edit: Fixed function2 parameters, return and other things. I apologize, was writing it quickly to illustrate my main question about freeing memory. Thanks for the corrections tips, but the real code is actually compiling correctly and everything.</p>
  
  <p>Edit2: After adding a simple loop at the end of main like suggested to use free(), I get the following errors.</p>
  
  <pre><code>==2216== LEAK SUMMARY:
  ==2216==    definitely lost: 44 bytes in 1 blocks
  ==2216==    indirectly lost: 0 bytes in 0 blocks
  ==2216==      possibly lost: 0 bytes in 0 blocks
  ==2216==    still reachable: 10,355 bytes in 34 blocks
  ==2216==         suppressed: 0 bytes in 0 blocks
  ==2216== 
  ==2216== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
  ==2216== 
  ==2216== 1 errors in context 1 of 2:
  ==2216== Invalid free() / delete / delete[] / realloc()
  ==2216==    at 0x563A: free (in /usr/local/Cellar/valgrind/3.8.1/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
  ==2216==    by 0x10000194E: main (in ./test.out)
  ==2216==  Address 0x7fff5fbf9dd0 is on thread 1's stack
  ==2216== 
  ==2216== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
  </code></pre>