Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidimensional array in C++ hang

I wanna to declare an array: int a[256][256][256] And the program hang. (I already comment out all other codes...) When I try int a[256][256], it runs okay.

I am using MingW C++ compiler, Eclipse CDT.

My code is: int main(){ int a[256][256][256]; return 0; }

Any comment is welcomed.

like image 287
Lily Avatar asked Dec 20 '25 00:12

Lily


1 Answers

This might happen if your array is local to a function. In that case, you'd need a stack size sufficient to hold 2^24 ints (2^26 bytes, or 64 MB).

If you make the array a global, it should work. I'm not sure how to modify the stack size in Windows; in Linux you'd use "ulimit -s 10000" (units are KB).

If you have a good reason not to use a global (concurrency or recursion), you can use malloc/free. The important thing is to either increase your stack (not a good idea if you're using threads), or get the data on the heap (malloc/free) or the static data segment (global).

Ideally you'd get program termination (core dump) and not a hang. I do in cygwin.

like image 66
Jonathan Graehl Avatar answered Dec 21 '25 15:12

Jonathan Graehl