title: "*** stack smashing detected ***: ./a.out terminated Aborted (core dumped)" - array insertion
body: <p>I got the following code over the Internet to insert an element in an array. My question is that &quot;how could the size of the array can be incremented especially​ at first insertion and garbage is printed at every execution of printing for loop?&quot;. I'm also eager to get details on the error I get.</p>
  <p>The code is</p>
  <pre><code>#include &lt;stdio.h&gt;
  void main() 
  {
      int k = 3, n = 5, i = 0, j = n;
      int LA[] = {1,3,5,7,8};
      printf(&quot;The original array elements are :\n&quot;);
      for(i = 0; i&lt;n; i++) {
          printf(&quot;%d &quot;,LA[i]);
      }
      n = n + 1;
      while( j &gt;= k){
          LA[j+1] = LA[j];
          j = j - 1;
      }
      LA[k] = 10;
      printf(&quot;\nThe array elements after insertion1 :\n&quot;);
      for(i = 0; i&lt;n; i++) {
          printf(&quot;%d &quot;,LA[i]);
      }
      n = n + 1;
      while( j &gt;= k){
          LA[j+1] = LA[j];
          j = j - 1;
      }
      LA[k] = 20;
      printf(&quot;\nThe array elements after insertion2 :\n&quot;);
      for(i = 0; i&lt;n; i++) {
          printf(&quot;%d &quot;,LA[i]);
      }
      n = n + 1;
      while( j &gt;= k){
          LA[j+1] = LA[j];
          j = j - 1;
      }
      LA[k] = 30;
      printf(&quot;\nThe array elements after insertion3 :\n&quot;);
      for(i = 0; i&lt;n; i++) {
          printf(&quot;%d &quot;,LA[i]);
      }
  }
  </code></pre>
  <p>The output is</p>
  <pre><code>The original array elements are :
  1 3 5 7 8 
  The array elements after insertion1 :
  1 3 5 10 7 8 
  The array elements after insertion2 :
  1 3 5 20 7 8 2087809280 
  The array elements after insertion3 :
  *** stack smashing detected ***: ./a.out terminated
  1 3 5 30 7 8 2087809280 -1077687568 Aborted (core dumped)
  </code></pre>
  <p>thanks for ur time.</p>