title: Weird behaviour with variable length arrays in struct in C
body: <p>I came across a concept which some people call a "Struct Hack" where we can declare a pointer variable inside a struct, like this:</p>
  
  <pre><code>struct myStruct{
      int data;
      int *array;
  };
  </code></pre>
  
  <p>and later on when we allocate memory for a <code>struct myStruct</code> using <code>malloc</code> in our <code>main()</code> function, we can simultaneously allocate memory for our <code>int *array</code> pointer in same step, like this:</p>
  
  <pre><code>struct myStruct *p = malloc(sizeof(struct myStruct) + 100 * sizeof(int));
  
  p-&gt;array = p+1;
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>struct myStruct *p = malloc(sizeof(struct myStruct));
  
  p-&gt;array = malloc(100 * sizeof(int));
  </code></pre>
  
  <p>assuming we want an array of size 100.</p>
  
  <p>The first option is said to be better since we would get a continuous chunk of memory and we can free that whole chunk with one call to free() versus 2 calls in the latter case.</p>
  
  <p>Experimenting, I wrote this:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct myStruct{
      int i;
      int *array;
  };
  
  int main(){
      /* I ask for only 40 more bytes (10 * sizeof(int)) */
  
      struct myStruct *p = malloc(sizeof(struct myStruct) + 10 * sizeof(int)); 
  
      p-&gt;array = p+1; 
  
      /* I assign values way beyond the initial allocation*/
      for (int i = 0; i &lt; 804; i++){
          p-&gt;array[i] = i;
      }
  
      /* printing*/
      for (int i = 0; i &lt; 804; i++){
          printf("%d\n",p-&gt;array[i]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I am able to execute it without problems, without any segmentation faults. Looks weird to me.</p>
  
  <p>I also came to know that C99 has a provision which says that instead of declaring an <code>int *array</code> inside a struct, we can do <code>int array[]</code> and I did this, using <code>malloc()</code> only for the struct, like</p>
  
  <pre><code>struct myStruct *p = malloc(sizeof(struct myStruct));
  </code></pre>
  
  <p>and initialising array[] like this</p>
  
  <pre><code>p-&gt;array[10] = 0; /* I hope this sets the array size to 10 
                      and also initialises array entries to 0 */
  </code></pre>
  
  <p>But then again this weirdness where I am able to access and assign array indices beyond the array size and also print the entries:</p>
  
  <pre><code>for(int i = 0; i &lt; 296; i++){ // first loop
      p-&gt;array[i] = i;
  }
  
  for(int i = 0; i &lt; 296; i++){ // second loop
      printf("%d\n",p-&gt;array[i]);
  }
  </code></pre>
  
  <p>After printing <code>p-&gt;array[i]</code> till <code>i = 296</code> it gives me a segmentation fault, but clearly it had no problems assigning beyond <code>i = 9</code>. 
  (If I increment 'i' till 300 in the first for loop above, I immediately get a segmentation fault and the program doesn't print any values.)</p>
  
  <p>Any clues about what's happening? Is it undefined behaviour or what?</p>
  
  <p>EDIT: When I compiled the first snippet with the command </p>
  
  <pre><code>cc -Wall -g -std=c11 -O    struct3.c   -o struct3
  </code></pre>
  
  <p>I got this warning:</p>
  
  <pre><code> warning: incompatible pointer types assigning to 'int *' from
    'struct str *' [-Wincompatible-pointer-types]
      p-&gt;array = p+1;
  </code></pre>