title: Why memset init partially?
body: <p>I'm trying to init a <code>int **a</code> with 0 by <code>memset</code>. But the result is some item is not zero.<br/>
  <strong>Source code</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;memory.h&gt;
  
  int main()
  {
      int aSize = 3;
      int **a = (int **)malloc(sizeof(int *)*aSize);
      for (int i=0; i&lt;aSize; i++) {
          a[i]=(int *)malloc(sizeof(int)*aSize);
      }
  
      memset(a[0], 0, aSize*aSize*sizeof(int));
  
      for (int i=0; i&lt;aSize; i++) {
          for (int j=0; j&lt;aSize; j++) {
              printf("%d ", a[i][j]);
          }
          printf("\n");
      }
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>0 0 0 
  0 0 0 
  0 1610612736 0
  </code></pre>
  
  <hr>
  
  <p><strong>Update 1</strong></p>
  
  <p>I'm trying to <code>malloc</code> contiugous in two ways as suggested. But in method 2 a exception thrown when init array. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt; // memset header changed
  // method 1
  int **a = malloc(sizeof(int *)*aSize);
  for (int i=0; i&lt;aSize; i++) {
      a[i]=malloc(sizeof(int)*aSize);
      memset(a[i], 0, aSize*sizeof(int));
  }
  
  for (int i=0; i&lt;aSize; i++) {
      for (int j=0; j&lt;aSize; j++) {
          printf("%d ", a[i][j]);
      }
      printf("\n");
  }
  
  for (int i=0; i&lt;aSize; i++) {
      free(a[i]);
  }
  
  free(a);
  
  // method 2
  a = malloc(sizeof(int *)*aSize + sizeof(int)*aSize);
  memset(a[0], 0, aSize*aSize*sizeof(int)); // Exception: EXC_BAD_ACCESS (code=1, address=0x1c)
  </code></pre>