title: Why do I receive a std::bad_alloc exception when running this code?
body: <p>When I run the code below, I receive an error like this:</p>
  
  <blockquote>
    <p>terminate called after throwing an instance of 'std::bad_alloc'<br>
    what():  std::bad_alloc</p>
    
    <p>This application has requested the Runtime to terminate it in an
    unusual way. Please contact the application's support team for more
    information.</p>
  </blockquote>
  
  <p>I think it may be caused by the "resize()" line, but I don't know how to fix it. Here is my code:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  long long n, l, r;
  string x[3];
  
  int main()
  {
      ios::sync_with_stdio(false);
      for (int i = 0; i &lt;= 2; i++)
          x[i].resize(x[i].max_size());
      x[0] = '0';
      x[1] = '1';
      cin &gt;&gt; n &gt;&gt; l &gt;&gt; r;
      for (register long long i = 2; i &lt;= n ; i++)
          x[i % 3] = x[(i - 2) % 3] + x[(i - 1) % 3];
      cout &lt;&lt; x[n % 3].substr(l, r - l + 1) &lt;&lt; endl;
      return 0;
  }
  </code></pre>