Array size limit?
Hi, this seems like such a silly problem, but I'm at a loss to figure out the cause.
in C++, trying to make a big array...
const double tempsize = 64*64*64;
double arr[tempsize];
Is there a limit on the number of elements an array can have? Is there a way around this?
tempsize shouldn't be a double. You use integer as array indices. The size of the array is available memory or the biggest number you can represent with the index variable type.
If you try to allocate an array on the stack, then your array size is limited by the remain stack space. Depending on your operating system and compiler you may be able to increase the default stack space. However, you should consider allocating your array on the heap.
Local arrays are limited to how much space is on the stack, which isn't very much. I think in Visual Studio, the default stack size is 2MB or something like that. So, large arrays should be allocated on the heap. There, your limit is determined by how much memory you have available and how fragmented it is.
If you're using double on a platform where double is 8 bytes you really shouldn't try to use an array that large on the stack.
Your array would be 2MiB in size which is over the default stacksize(1MiB) in windows.
Thus if you want to use arrays that large you have to either allocate them on the heap or increase the stack size (You can do that by changing the linker settings, 32MiB/Thread is max in Windows)
Your array would be 2MiB in size which is over the default stacksize(1MiB) in windows.
Thus if you want to use arrays that large you have to either allocate them on the heap or increase the stack size (You can do that by changing the linker settings, 32MiB/Thread is max in Windows)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement