title: Working of malloc in C
body: <p>I am a beginner with C. I am wondering, how's <code>malloc</code> working.
  Here is a sample code, I wrote on while trying to understand it's working. </p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int main() {
    int i;
    int *array = malloc(sizeof *array);
    for (i = 0; i &lt; 5; i++) {
      array[i] = i+1;
    }
    printf("\nArray is: \n");
    for (i = 0; i &lt; 5; i++) {
      printf("%d ", array[i]);
    }
    free(array);
    return 0;
  }
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>Array is: 
  1 2 3 4 5
  </code></pre>
  
  <p>In the program above, I have only allocated space for 1 element, but the array now holds 5 elements. So as the programs runs smoothly without any error, what is the purpose of <code>realloc()</code>.</p>
  
  <p>Could anybody explain why?</p>
  
  <p>Thanks in advance.</p>