continuous memory allocation for structure with pointer fields
<p>I have a structure like</p>

<p><code>struct mystruct {
      int x;
      int size_of_data;
      int *data;
}</code></p>

<p>and I want to allocate memory at continuous memory area for mystruct and the data.
I tried <code>malloc(sizeof(struct mystruct)+size_of_data*sizeof(int))</code> but I got a segmentation fault.</p>

<p>I also tried:</p>

<pre><code>struct mystruct *var = malloc(sizeof(*var))
var-&gt;data = malloc(size_of_data*sizeof(int)) 
</code></pre>

<p>but the problem is that the allocator does not guarantee that the <code>var-&gt;data</code> will be contiuous to the whole struct, but I strongly need to be continuous. Any ideas?</p>

<p><code>mystruct</code> needs to stay unchanged</p>