user3127680
user3127680

Reputation: 373

Is it possible to store 10 million numbers in array?

I want to know how many numbers can you store in array?

srand (time(NULL));
int array[10000000];
for(int i = 0; i < 10000000; i++){
    array[i] = (rand() % 10000000) + 1;
}

Every time I want to store 10.000.000 numbers in array my program crashed (Eclipse). I even tryed Visual Studio and it crashed to.

So i want to know how many numbers can I store in array or is something wrong with my code?

Upvotes: 9

Views: 27228

Answers (3)

Collin
Collin

Reputation: 12287

There are a couple places you could put your array in memory. The most common difference we think about is the stack and the heap.

The stack is how the computer keeps track of which function you're in, how to return from the function, and the local variables. It is often of limited size. The exact limit depends on your platform, and perhaps how you compiled your program.

The heap is another area in memory, where the compiler usually stores things you've allocated with the new keyword. This is often much larger, and capable of storing a big array such as yours. The downside to keeping things on the heap is that you have to remember to delete them at the appropriate time.

In your example, you are declaring a 10,000,000 element array on the stack. If you wanted to declare that array on the heap, you would do it like this:

srand (time(NULL));
int* array = new int[10000000];
for(int i = 0; i < 10000000; i++){
    array[i] = (rand() % 10000000) + 1;
}

//Sometime later...
delete[] array;

However, C++ gives us better tools for this. If you want a big array, use std::vector.

srand (time(NULL));
std::vector<int> array(10000000);
for(std::size_t i = 0; i < array.size(); i++){
    array[i] = (rand() % 10000000) + 1;
}

Now, your std::vector is on the stack, but the memory it controls is on the heap. You don't have to remember to delete it later, and your program doesn't crash.

Upvotes: 2

JaredPar
JaredPar

Reputation: 754763

The language is more than capable of storing 10,000,000 values in an array. The problem here though is you've declared the 10,000,000 elements to exist on the stack. The size of the stack is implementation dependent but most stacks simply don't have enough space for that many elements. The heap is a much better place for such an array

int* array = new int[10000000];
for(int i = 0; i < 10000000; i++){
    array[i] = (rand() % 10000000) + 1;
}
...
delete[] array;

Upvotes: 7

John Zwinck
John Zwinck

Reputation: 249223

You can store as many numbers as you have memory for, but you cannot do it like that. The reason your program crashes is that you are using an "automatic" variable, which is allocated on the "stack." The stack is much more limited in size than the "heap" typically, so using automatic variables of such large size may result in a...wait for it...

STACK OVERFLOW!

Instead, try this:

int* array = new int[10000000];

Then after using it:

delete[] array;

Step two will be to learn about smart pointers; you can use something like boost::scoped_array for this case, but there are lots of options depending on which libraries you prefer (or if you have C++11).

If you have C++11 you can use "RAII" to avoid needing to remember when and where to call delete. Just do this to allocate the array:

std::unique_ptr<int[]> array(new int[10000000]);

Or just use a vector, which always allocates its contents dynamically ("on the heap", loosely speaking):

std::vector<int> array(10000000); // 10000000 elements, all zero

Upvotes: 31

Related Questions