title: Malloc a 1 d array of pointers error
body: <p>I am trying to malloc a 1 d array of pointers following a 1d array of struct type entry. </p>
  
  <p>While making each pointer points to the correspondent element of the 1d array, I hit the problem.</p>
  
  <p>The pointers stores at the beginning of the memory and each pointer points to the address of each entry. </p>
  
  <p>Below is a simple illustration:</p>
  
  <pre><code>--------------------------
  ptr1|ptr2|ptr3|ptr4|ptr5
  --------------------------
  entry1
  --------------------------
  entry2
  --------------------------
  entry3
  --------------------------
  entry4
  --------------------------
  entry5
  --------------------------
  
  
  each pointer in the array of pointers points to entry 1- 5 accordingly.
  </code></pre>
  
  <p>Here is the code, </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define MAX_LAST_NAME_SIZE 16
  #define TABLE_SIZE 131072  //2^17
  
  typedef struct __PHONE_BOOK_DETAIL {   //not really matter in this question
      char firstName[16];
      char city[16];
      char zip[5];
  
  } __PHONE_BOOK_DETAIL;
  
  typedef struct __PHONE_BOOK_ENTRY {    // the size is 16 + 8 +8 =32byte
      char lastName[MAX_LAST_NAME_SIZE];
  
      __PHONE_BOOK_DETAIL *detail;
  
      struct __PHONE_BOOK_ENTRY *pNext;
  
  } entry;
  
  int main()
  {
      int i;
  
      entry  **pHead;
      entry  *e; 
      pHead = (entry **) malloc(sizeof(entry *)*TABLE_SIZE + sizeof(entry)* TABLE_SIZE);
  
      for(i=0;i&lt;TABLE_SIZE;i++){
          pHead[i]=  (pHead+ TABLE_SIZE) + sizeof(entry)*i;
          printf("i=%d , phead[i]=%p &amp;phead[i]=%p, sizeof (entry)=%d sizeof(e)=%d \n",i, pHead[i],&amp;pHead[i],sizeof(entry),sizeof(e));
          //pHead[i]-&gt;pNext=NULL;
      }
      return 0;
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>i=0 , phead[i]=6b597010 &amp;phead[i]=6b497010, sizeof (entry)=32 sizeof(e)=8
  i=1 , phead[i]=6b597110 &amp;phead[i]=6b497018, sizeof (entry)=32 sizeof(e)=8
  i=2 , phead[i]=6b597210 &amp;phead[i]=6b497020, sizeof (entry)=32 sizeof(e)=8
  i=3 , phead[i]=6b597310 &amp;phead[i]=6b497028, sizeof (entry)=32 sizeof(e)=8
  i=4 , phead[i]=6b597410 &amp;phead[i]=6b497030, sizeof (entry)=32 sizeof(e)=8
  i=5 , phead[i]=6b597510 &amp;phead[i]=6b497038, sizeof (entry)=32 sizeof(e)=8
  i=6 , phead[i]=6b597610 &amp;phead[i]=6b497040, sizeof (entry)=32 sizeof(e)=8
  i=7 , phead[i]=6b597710 &amp;phead[i]=6b497048, sizeof (entry)=32 sizeof(e)=8
  i=8 , phead[i]=6b597810 &amp;phead[i]=6b497050, sizeof (entry)=32 sizeof(e)=8
  i=9 , phead[i]=6b597910 &amp;phead[i]=6b497058, sizeof (entry)=32 sizeof(e)=8
                        ....
  </code></pre>
  
  <p>The program crashes when i = 16369. It seems to be memory access violation.</p>
  
  <p>My CPU is 64 bit, so the pointer size is 8 bytes.</p>
  
  <p>However, while I am expecting the size of entry is 32byte, you can see the increment of phead[i] is 256. Can anyone explain that?</p>
  
  <p>Thanks!</p>