title: why dereferencing a C pointer after "free" gives value 0
body: <p>Below is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int main()
  {
      int *p;
      p=(int *)malloc(sizeof(int));
      *p=5;
      printf("Before freeing=%p\n",p);
      printf("Value of p=%d\n",*p);
      //making it dangling pointer
      free(p);
      printf("After freeing =%p\n",p);
      printf("Value of p=%d\n",*p);
      return 0;
  }
  </code></pre>
  
  <p>Below is the output:</p>
  
  <pre><code>Before freeing=0x1485010
  Value of p=5
  After freeing =0x1485010
  Value of p=0
  </code></pre>
  
  <p>After freeing the pointer, dereferencing gives the output "0"(zero).</p>
  
  <p>Below is another code that also gives '0'</p>
  
  <pre><code>include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int main()
  {
  int *p;
  p=(int *)malloc(sizeof(int));
  
  printf("Before freeing=%p\n",(void *)p);
  printf("Value of p=%d\n",*p);
  return 0;
  }
  </code></pre>
  
  <p>In this i have not freed the memory , just allocated it , still it gives '0' .
  Is it like the default value of every uninitialized pointer is '0'??</p>
  
  <p>why is it so ?</p>