title: Why does malloc initialize the values to 0 in gcc?
body: <p>Maybe it is different from platform to platform, but</p>
  <p>when I compile using gcc and run the code below, I get 0 every time in my ubuntu 11.10.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      double *a = malloc(sizeof(double)*100)
      printf(&quot;%f&quot;, *a);
  }
  </code></pre>
  <p>Why do malloc behave like this even though there is calloc?</p>
  <p>Doesn't it mean that there is an unwanted performance overhead just to initialize the values to 0 even if you don't want it to be sometimes?</p>
  <hr />
  <p>EDIT: Oh, my previous example was not initiazling, but happened to use &quot;fresh&quot; block.</p>
  <p>What I precisely was looking for was why it initializes it when it allocates a large block:</p>
  <pre><code>int main()
  {
      int *a = malloc(sizeof(int)*200000);
      a[10] = 3;
      printf(&quot;%d&quot;, *(a+10));
  
      free(a);
  
      a = malloc(sizeof(double)*200000);
      printf(&quot;%d&quot;, *(a+10));
  }
  
  OUTPUT: 3
          0 (initialized)
  </code></pre>
  <p>But thanks for pointing out that there is a SECURITY reason when mallocing! (Never thought about it). Sure it has to initialize to zero when allocating fresh block, or the large block.</p>