title: Malloc, how it allocate the address?
body: <p>I know that the malloc, allocate the contigous blocks of memory and return the first address of the allocation, which we keep in a pointer an use it.</p>
  
  <p>I have the following question:</p>
  
  <p>I discovered that if I allocate a number of bytes less than necessary or if try to access something which exceeds the bytes allocated the program still work.</p>
  
  <pre><code>int *ptr;
  ptr = (int*)malloc(sizeof(int) * 2);
  *ptr = 1;
  *(ptr + 1) = 2;
  *(ptr + 2) = 3;
  </code></pre>
  
  <p>After my mind, <code>*(ptr + 2) = 3;</code> should not work but it still work. I think because <code>*(ptr + 1)</code> is followed by <code>*(ptr + 2)</code> in memory.</p>
  
  <p>My question is why it work and if it work like this why we don't use malloc for allocate only the first element of anything?</p>