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!
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.
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]);
An array of different things or people is a large number or wide range of them.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With