title: Program giving bad alloc error
body: <p>I have writen a program on generating a square filled matrix filled with element from 1 to n^2 in spiral order.It is giving  a bad alloc error.
  The error message is something like this terminate called after throwing an instance of 'std::bad_alloc'
    what():  std::bad_alloc
  Aborted (core dumped)
  Can someone suggest me how to remove it and remove such errors using gdb</p>
  
  <p>Here is the code</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  using namespace std;
  
  
  
  vector&lt;vector&lt;int&gt; &gt; generateMatrix(int A) 
  {
  if(A&lt;=0)//if value of A return null matrix
    return vector&lt;vector&lt;int&gt; &gt;();
  
  vector&lt;int&gt;v(A,0);   
  vector&lt;vector&lt;int&gt; &gt;result(A,v);
  
  
  
       int i, k = 0, l = 0;
       int n = A, m = A;
  /*  k - starting row index
      m - ending row index
      l - starting column index
      n - ending column index
      i - iterator
  */
  int c = 1;//used for numbering
  
  while (k &lt; m &amp;&amp; l &lt; n)
  {
      /* Print the first row from the remaining rows */
      for (i = l; i &lt; n; ++i)
      {
          result[k][i]=c++;
      }
      k++;
  
      /* Print the last column from the remaining columns */
      for (i = k; i &lt; m; ++i)
      {
          result[i][n-1]=c++;
      }
      n--;
  
      /* Print the last row from the remaining rows */
      if ( k &lt; m)
      {
          for (i = n-1; i &gt;= l; --i)
          {
              result[m-1][i]=c++;
          }
          m--;
      }
  
      /* Print the first column from the remaining columns */
      if (l &lt; n)
      {
          for (i = m-1; i &gt;= k; --i)
          {
          result[i][l]=c++;
          }
          l++;    
      }        
  }    
  }
  
  int main()
  {   
       int n;
  
  cin&gt;&gt;n;
  
  vector&lt;vector&lt;int&gt; &gt;X(n,vector&lt;int&gt;(n));
  X = generateMatrix(n);
  
  for(int i  = 0; i &lt; n; i++)
  {
  cout&lt;&lt;'\n';
      for(int j = 0;j&lt;n;j++)
      {   cout&lt;&lt;X[i][j]&lt;&lt;'\t';}
  }
  
  return 0;}
  </code></pre>