title: What is the maximum index for c++ new[] operator?
body: <p>I thought that the maximum index for an array is a signed int, 2147483647. However reading online, I found posts stating that for a 32 bit system an array can have 4294967295 elements, unsigned int. And for a 64 bit system, an array can have 2^64 elements, 18446744073709551616.</p>
  
  <p>When I tried to compile and run the following program, I got a warning:</p>
  
  <blockquote>
    <p>[Warning] integer overflow in expression [-Woverflow]</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
     int maxindex = 2147483647+1;
  
     char* p = new char [maxindex];
  
     for(int i = 0; i &lt; maxindex; i++)
     {
         p[i] = 65;   
     }
  
     cout&lt;&lt;"Value at index "&lt;&lt;maxindex-1&lt;&lt;" is "&lt;&lt;p[maxindex-1]&lt;&lt;endl;
  
     delete[] p;
  
     return 0;
  }
  </code></pre>
  
  <p>When I ran the program, it terminated immediately:</p>
  
  <blockquote>
    <p>terminate called after throwing an instance of 'std::bad_alloc'
      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>If I change <code>int maxindex = 2147483647+1;</code> to <code>int maxindex = 2147483647;</code> the program runs with no problems. What am I doing wrong and is the limit for new[] index 2^64, 18446744073709551616, or 2147483647?</p>