title: Code showing error *** stack smashing detected ***: ./a.out terminated Aborted (core dumped)
body: <p>This code is not running properly and showing the following error at runtime: </p>
  
  <blockquote>
    <p>stack smashing detected: ./a.out terminated<br>
    Aborted (core dumped)</p>
  </blockquote>
  
  <p>However, if we declare temp with i, j it works. The error is also shown if we declare temp in the if block.  The error is not shown if we declare the array size explicitly. I am compiling my code with the GNU compiler.</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      int i, j;
      int a[] = {3, 2, 4, 1};
      int temp;
  
      for(i = 0; i &lt; 4; i++)
      {
          for(j = 0; j &lt; 4 - i; j++)
          {
              if(a[j] &gt; a[j + 1])
              {
                  int temp;
                  temp = a[j];
                  a[j] = a[j + 1];
                  a[j + 1] = temp;
              }
          }
      }
  
      for(i = 0; i &lt; 4; i++)
      {
          cout &lt;&lt; a[i] &lt;&lt; endl;
      }
  
      return 2;
  }
  </code></pre>