title: In C, why are the first members of a struct frequently "reset" to 0 when it is deallocated with free()?
body: <h2>The setup</h2>
  <p>Let's say I have a <code>struct father</code> which has member variables such as an <code>int</code>, and another <code>struct</code>(so <code>father</code> is a nested <code>struct</code>). This is an example code:</p>
  <pre><code>struct mystruct {
      int n;
  };
  
  struct father {
      int test;
      struct mystruct M;
      struct mystruct N;
  };
  </code></pre>
  <p>In the main function, we allocate memory with <code>malloc()</code> to create a new struct of type <code>struct father</code>, then we fill it's member variables and those of it's children:</p>
  <pre><code>    struct father* F = (struct father*) malloc(sizeof(struct father));
      F-&gt;test = 42;
      F-&gt;M.n = 23;
      F-&gt;N.n = 11;
  </code></pre>
  <p>We then get pointers to those member variables from outside the <code>struct</code>s:</p>
  <pre><code>    int* p = &amp;F-&gt;M.n;
      int* q = &amp;F-&gt;N.n;
  </code></pre>
  <p>After that, we print the values before and after the execution of <code>free(F)</code>, then exit:</p>
  <pre><code>    printf(&quot;test: %d, M.n: %d, N.n: %d\n&quot;, F-&gt;test, *p, *q);
      free(F);
      printf(&quot;test: %d, M.n: %d, N.n: %d\n&quot;, F-&gt;test, *p, *q);
      return 0;
  </code></pre>
  <p>This is a sample output(*):</p>
  <pre><code>test: 42, M.n: 23, N.n: 11
  test: 0, M.n: 0, N.n: 1025191952
  </code></pre>
  <p>*: Using gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0</p>
  <p>Full code on pastebin: <a href="https://pastebin.com/khzyNPY1" rel="nofollow noreferrer">https://pastebin.com/khzyNPY1</a></p>
  <h2>The question</h2>
  <p>That was the test program that I used to test <em>how memory is deallocated</em> using free(). My idea(from reading K&amp;R &quot;8.7 Example - A Storage Allocator&quot;, in which a version of <code>free()</code> is implemented and explained) is that, when you <code>free()</code> the <code>struct</code>, you're pretty much just telling the operating system or the rest of the program that you won't be using that particular space in memory that was previously allocated with <code>malloc()</code>. So, after freeing those memory blocks, there should be garbage values in the member variables, right? I can see that happening with <code>N.n</code> in the test program, but, as I ran more and more samples, it was clear that in the overwhelming majority of cases, these member variables are &quot;reset&quot; to 0 more than any other &quot;random&quot; value. My question is: <strong>why is that?</strong> Is it because the stack/heap is filled with zeroes more frequently than any other value?</p>
  <hr />
  <p>As a last note, here are a few links to related questions but which <em>do not</em> answer my particular question:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/13590812/c-freeing-structs">C - freeing structs</a></li>
  <li><a href="https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc">What REALLY happens when you don't free after malloc?</a></li>
  </ul>