-1

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.

When I tried to compile and run the following program, I got a warning:

[Warning] integer overflow in expression [-Woverflow]

#include <iostream>
using namespace std;

int main()
{
   int maxindex = 2147483647+1;

   char* p = new char [maxindex];

   for(int i = 0; i < maxindex; i++)
   {
       p[i] = 65;   
   }

   cout<<"Value at index "<<maxindex-1<<" is "<<p[maxindex-1]<<endl;

   delete[] p;

   return 0;
}

When I ran the program, it terminated immediately:

terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

If I change int maxindex = 2147483647+1; to int maxindex = 2147483647; the program runs with no problems. What am I doing wrong and is the limit for new[] index 2^64, 18446744073709551616, or 2147483647?

WhatIf
  • 653
  • 2
  • 8
  • 18
  • 3
    because int on your platform can only hold 32 bits(4 bytes), so that 2147483647+1 equals to -2147483648, and new probably doesnt like negative sizes. use long long, or unsigned int if you want higher than 2147483647 – Creris Sep 05 '15 at 14:48
  • 7
    "What is the maximum index for c++ new[] operator?" – it depends on your architecture, and the available free memory. Also, it's not an index, it's a size. – The Paramagnetic Croissant Sep 05 '15 at 14:49
  • You should get used to unsigned integers (size_t), too –  Sep 05 '15 at 14:53
  • 2
    "When I tried to compile and run the following program, I got a warning". You need to understand the warning and fix it, rather than proceed with running your program and wonder why it gives you somewhat unexpected results. – n. m. could be an AI Sep 05 '15 at 15:06
  • I changed int maxindex = 2147483647+1; to unsigned int maxindex = 2147483647+1; Now it runs but I'm still getting the warning! [Warning] integer overflow in expression [-Woverflow] – WhatIf Sep 05 '15 at 15:17
  • 1
    possible duplicate of [Is there a max array length limit in C++?](http://stackoverflow.com/questions/216259/is-there-a-max-array-length-limit-in-c) – phuclv Sep 05 '15 at 15:17
  • size of a type is `size_t`, not `unsigned int` – phuclv Sep 05 '15 at 15:22
  • You're asking about the maximum *size*, not the maximum *index*. – juanchopanza Sep 05 '15 at 15:26
  • Got it! Needed to append a u after the numbers unsigned int maxindex = 2147483647u+1u; No warnings now – WhatIf Sep 05 '15 at 15:37

1 Answers1

3

In C and C++, the maximum size an array can have is of type size_t. So you should be using size_t variables when you are declaring arrays and iterating over them.

std::size_t can store the maximum size of a theoretically possible object of any type (including array) [...] std::size_t can safely store the value of any non-member pointer [...] When indexing C++ containers, such as std::string, std::vector, etc, the appropriate type is the member typedef size_type provided by such containers. It is usually defined as a synonym for std::size_t.

Then, the amount of virtual memory available to your process dictates how much memory you are allowed to allocate on the heap (using the new operator), and on the stack using static arrays.

The exception you get, std::bad_alloc, is thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space, typically because there is not enough virtual memory available.

Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71