title: C - freeing structs
body: <p>Let's say I have this struct</p>
  
  <pre><code>typedef struct person{
      char firstName[100], surName[51]
  } PERSON;
  </code></pre>
  
  <p>and I am allocating space by malloc and filling it with some values</p>
  
  <pre><code>PERSON *testPerson = (PERSON*) malloc(sizeof(PERSON));
  strcpy(testPerson-&gt;firstName, "Jack");
  strcpy(testPerson-&gt;surName, "Daniels");
  </code></pre>
  
  <p>What is the correct and safe way to free all memory taken by that struct? Is "free(testPerson);" enough or do I need to free each struct's attribute one by one?</p>
  
  <p>It leads me to another question - how are structures stored in memory? I noticed a strange behaviour - when I try to print structure address it's equal to it's first attribute's address.</p>
  
  <pre><code>printf("Structure address %d == firstName address %d", testPerson, testPerson-&gt;firstName);
  </code></pre>
  
  <p>Which means that this
      free(testPerson)
  should be equal to this
      free(testPerson->firstName);</p>
  
  <p>and that's not what I want to do.</p>
  
  <p>Thanks</p>