title: Why does free not clear out a pointers value?
body: <p>I was trying to explain how free works to a friend, so I thought up the following code example to try and explain it (Complex, I know):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
  
    char *stringPtr = NULL;
  
    //Pass the size of the string in bytes to malloc
    stringPtr = (char *)malloc(10 * sizeof(char));
  
    strcpy(stringPtr, "A String. ");
    printf("%s", stringPtr);
    free(stringPtr);
  
    printf("%s", stringPtr);
  
    return 0;
  }
  </code></pre>
  
  <p>Thinking that this code would free up the memory allocated on the heap, I assumed that if I then went to print out the string that was assigned to the pointer, it would then do something weird or crash the program since I assumed it would try to output a null value; however when I ran it, it ended up just outputting the string twice. I figured I'd step through it to see why, because in my mind I thought maybe it copied the string to the stack or something and the pointer would then be pointing to a new memory address and the old memory got cleaned up after all (not that that scenario would make any sense, but hey, I didn't know what else it would be), but when I stepped through the code not only did it not free up the memory, the pointer actually was still pointing to the same memory address <em>and</em> it had that same value. </p>
  
  <p>As I looked into it people said that you need to assign the pointer a null value after calling free, but my question is why? Shouldn't free just deallocate that memory and set the pointer to null? What's the point in preserving the value? Or am I just not understanding something?  </p>