Putting An Array in the Heap

Mar 11, 2012 at 8:20pm
Hi all,

I am writing some code that requires me to create a few huge arrays of double precession floating points, too large to be put on the stack (at least I think thats my problem, I'm new at c++ coding). In my current code, the largest of the arrays should require 176 MB to store, and the size of the array needs to be able scale up from there. My code runs fine if I scale down the size of the arrays. A friend of mine mentioned that putting the arrays in the heap may solve the problem, I just am not sure how to go about doing that in code.

Thanks for your help!
Patcho
Mar 11, 2012 at 8:33pm
Instead of:
double hugeArray[176*1024]; // 176 MB
write this:
double* hugeArray = new double[176*1024];
Then use "hugeArray" like a normal array.

Also remember that you must call
delete[] hugeArray; // Brackets after the "delete" are necessary!
after you're done using "hugeArray", so that you can free the memory.
Mar 11, 2012 at 8:52pm
Thanks for the help. Is there any way to do it when the size of the array is a variable. I can do the computation for array by hand, but it is much easier to do the computation using c++ when I scale the array size up.
Mar 11, 2012 at 8:56pm
Use standard container std::vector<double>
Topic archived. No new replies allowed.