Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store 10 million numbers in array?

Tags:

c++

arrays

random

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?

like image 608
user3127680 Avatar asked Mar 22 '14 14:03

user3127680


People also ask

How many numbers can an array store?

"We can store elements only up to a [10000000] (10^7) in a array of integers" Where did you get such number, from?

What is the maximum possible size of an array?

The theoretical maximum Java array size is 2,147,483,647 elements.

Can you make an array size 10 9?

The memory limits for the SPOJ judge permit array (or vector )length only upto 10^7. There is no option but to reduce memory usage. You dont need to store all values upto 10^9.

Can you make an array size 10 7?

array size 10^7 to 10^8 declared globally(i.e. on heap) is possible…if u declare nething in the main or in ne fxn…it goes into the stack which has a smaller size hence ur 10^7 array did not work out… try declaring it globally and it should work…


2 Answers

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
like image 83
John Zwinck Avatar answered Oct 07 '22 06:10

John Zwinck


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.

like image 43
Collin Avatar answered Oct 07 '22 06:10

Collin