Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create too large array in C++, how to solve?

Tags:

c++

Recently, I work in C++ and I have to create a array[60.000][60.000]. However, i cannot create this array because it's too large. I tried float **array or even static float array but nothing is good. Does anyone have an ideas? Thanks for your helps!

like image 248
Kingfisher Phuoc Avatar asked Mar 02 '12 03:03

Kingfisher Phuoc


People also ask

How do you declare a large array?

Usually you need to create such an array dynamically on the heap. int *integer_array = (int*)malloc(2000000 * sizeof(int)); float *float_array = (float*)malloc(2000000 * sizeof(float)); The array might be too large for stack allocation, e.g. if used not globally, but inside a function.

How do you find out how big an array is?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

What is a big array?

An array of different things or people is a large number or wide range of them.

Can you increase the size of an array C++?

Once an array has been allocated, there is no built-in mechanism for resizing it in the C++ programming language. Therefore, we can avoid this problem by dynamically generating a new array, copying over the contents, and then deleting the old array.


2 Answers

A matrix of size 60,000 x 60,000 has 3,600,000,000 elements.

You're using type float so it becomes:

60,000 x 60,000 * 4 bytes = 14,400,000,000 bytes ~= 13.4 GB

Do you even have that much memory in your machine?


Note that the issue of stack vs heap doesn't even matter unless you have enough memory to begin with.


Here's a list of possible problems:

  • You don't have enough memory.
  • If the matrix is declared globally, you'll exceed the maximum size of the binary.
  • If the matrix is declared as a local array, then you will blow your stack.
  • If you're compiling for 32-bit, you have far exceeded the 2GB/4GB addressing limit.
like image 104
Mysticial Avatar answered Oct 27 '22 11:10

Mysticial


Does "60.000" actually mean "60000"? If so, the size of the required memory is 60000 * 60000 * sizeof(float), which is roughly 13.4 GB. A typical 32-bit process is limited to only 2 GB, so it is clear why it doesn't fit.

On the other hand, I don't see why you shouldn't be able to fit that into a 64-bit process, assuming your machine has enough RAM.

like image 44
Branko Dimitrijevic Avatar answered Oct 27 '22 11:10

Branko Dimitrijevic