Problem with allocating memory in c++ and pointers : <p>So I am learning dynamic memory allocation nowadays and stuck in a problem(if it is).</p>

<pre><code>int *p;
p=(int*)calloc(5,sizeof(int));
</code></pre>

<p>According to my intution p should point to memory location of 5*4 = 20byte and these 20bytes only belong to p.
for *(p+0), <em>(p+1)....</em>(p+4) everything is fine.
but when i do *(p+5) or *(p+12)=100; This should give me an error as i have exceeded the memory block allocated to *p.</p>

<p>The confusion is : Why we need to even allocate memory when we can just do like :</p>

<pre><code>int *p
int s[]={1,2,3}
p=&amp;s[0]
//and then make an array of length &gt; s
*(p+4)=....something
</code></pre>

<p>In every article i am seeing they are allocating memory before assigning . what's the need. I hope u understand my confusion.</p>
