title: How does free() affect the memory address on the heap?
body: <p>This assignment asks us to allocate two int-type variables using <code>malloc()</code> (named <code>var1</code> and <code>var2</code>), print the addresses of each variable (the address of the pointer on the stack and the address on the heap), then use <code>free()</code> to deallocate <code>var1</code>, print the addresses again, then allocate another space in the heap for <code>var1</code> and print the addresses a third time.  I believe that the instructor is trying to show us that the heap address for <code>var1</code> is supposed to change, but it always stays the same... unless I remove <code>free(var1)</code> from the code.  The instructor did a similar demonstration, but did not use <code>free()</code> to deallocate any variables, so we never saw how this was supposed to work. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void main()
  {
  
  int *var1 = (int*)malloc(sizeof(int)); 
  *var1 = 1000;                   
  int *var2 = (int*)malloc(sizeof(int)); 
  *var2 = 2000;
  
  printf("Addresses of var1\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var1, var1); 
  printf("Addresses of var2\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var2, var2);
  
  free(var1);
  
  printf("AFTER DEALLOCATING var1 FROM THE HEAP\n");
  printf("Addresses of var1\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var1, var1); 
  printf("Addresses of var2\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var2, var2);
  
  var1 = (int*) malloc(sizeof(int));
  *var1 = 1500;
  
  printf("NEW MEMORY ADDRESS ALLOCATED FOR var1\n");
  printf("Addresses of var1\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var1, var1); 
  printf("Addresses of var2\n");
  printf("Pointer on stack: %p / Heap: %p\n\n", &amp;var2, var2); 
  
  }
  </code></pre>
  
  <p>This code results in this output:</p>
  
  <pre><code>Addresses of var1
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  
  AFTER DEALLOCATING var1 FROM THE HEAP
  Addresses of var1
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  
  NEW MEMORY ADDRESS ALLOCATED FOR var1
  Addresses of var1
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  </code></pre>
  
  <p>As you can see, the heap address does not change for <code>var1</code> when I deallocate it, and it doesn't change when I allocate memory space for <code>var1</code> again.  However, if I simply remove the <code>free(var1)</code> line from the program, it simply assigns a second memory space for <code>var1</code> and points to that on the heap, which DOES have a different memory address:</p>
  
  <pre><code>Addresses of var1 
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  
  AFTER DEALLOCATING var1 FROM THE HEAP
  Addresses of var1
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  
  NEW MEMORY ADDRESS ALLOCATED FOR var1
  Addresses of var1
  Pointer on stack: 0xffffcbf8 / Heap: 0x600000420
  
  Addresses of var2
  Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
  </code></pre>
  
  <p>(Just to be clear, all I did was remove <code>free(var1)</code> from the previous code, so the "AFTER DEALLOCATING var1" section now shows the exact same heap address as the previous set, but it DOES change the heap address of var1 in the third section.)</p>
  
  <p>Can anybody tell me what is happening here?  The only logical explanation I can come up with is that when I'm using <code>free()</code> to deallocate <code>var1</code> and then printing the address, it's simply printing the LAST address that it pointed to, and then when I'm allocating memory for <code>var1</code> the second time, it's simply "backfilling"  the previous address with the new value of <code>var1</code>.  Does this make sense?  Do I have errors in my code, or is this just how C behaves when deallocating memory for a variable and then reallocating it?  </p>