4

When i do:

vector<double> myVect(120000000, 0);

I can make the vector hold seemingly as many elements as i want. However, when i do:

double myArray[120000];

I am limited to somewhere around 120000-130000 elements before my program crashes. Is there something screwy going on or are arrays really this limited?

Rob Allsopp
  • 3,309
  • 5
  • 34
  • 53

6 Answers6

7

Arrays are not themselves limited to any fixed size, but arrays that you allocated in the automatic storage (commonly known as "the stack") are limited to the size of the stack. When you allocate large arrays in the static storage, you can make much larger arrays. Same goes for dynamic allocations: whatever the size of the vector that you could allocate without triggering a memory overflow, you can make an array of the same size with the new operator.

For example, you can do this without triggering stack overflow:

static double myArray[120000000]; // static memory area

or this:

double *myArray = new double[120000000]; // dynamic memory area
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @Thanks a Lot. Was Searching For This For Ages. – Suraj Jain Aug 15 '16 at 22:38
  • Can You Tell Me What is the maximum limit of static variable. ? . How much memory can it have. – Suraj Jain Aug 15 '16 at 22:39
  • @SurajJain That depends on a lot of things in your system - the total amount of memory, the OS, the compiler in use, and so on. – Sergey Kalinichenko Aug 15 '16 at 23:01
  • if i have 8 gb of ram. I heard that static variable can have up to 7 gb of ram. is that correct ? And how much can stack have ? And Heap ? I mean Can you tell me what is the order which one is bigger and which is smaller ? Not actual memory just comparing – Suraj Jain Aug 16 '16 at 16:41
0

vector allocates the memory on heap, while myArray in your example allocates the memory on stack which may cause stack overflow.

The stack is mainly used to store function parameters, return values, etc, and not supposed to store large amount of data. That's why it usually has a limited size specified by OS.

The heap, on the other hand, is quite different. It can "grow" depending on its usage. If heap uses up all memory, it may use swap space on disk. So it can be much larger than stack.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
0

In myArray you're trying to allocate 120000 on the stack (thus crashing it) whereas on the myVect, you're doing it on the heap.

Jacob
  • 34,255
  • 14
  • 110
  • 165
0

The amount of stack space your program can use is limited. If you allocate the array on the heap (using the new operator) everything should be fine. std::vector also allocates the objects on the heap.

tux21b
  • 90,183
  • 16
  • 117
  • 101
0

array and std::vector are different in C++. ‘Array' is statically allocated which means it takes memory from the stack. However std::vector is dynamically allocated which means it gets its memory from so called "free store" or "heap".

That's why you can have a large vector but a limit-sized array.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
CS Pei
  • 10,869
  • 1
  • 27
  • 46
0

Each double, on almost all modern C++ compilers, is 8 bytes long. An array of them will consume:

120000 * 8 = 960,000 
130000 * 8 = 1,040,000

This implies that the size of each stack frame in your program is roughly 1 MB. Since arrays (and all other "automatic variables") are stored on the stack, you're limited to that 1 MB per function. Vectors, and other objects, use heap memory, which allows you several GBs of space.

If you need to increase the stack size, check out this thread: Change stack size for a C++ application in Linux during compilation with GNU compiler

Community
  • 1
  • 1
utopianheaven
  • 1,267
  • 7
  • 18
  • Even though that's completely correct, this is very risky advice, since the size of the stack isn't _really_ the problem. – devsnd Aug 15 '13 at 00:43