views:

290

answers:

3

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.

A: 

Maybe you don't have 16MB of free continuous memory? Kind of hard to imagine but possible...

bkritzer
@bkritzer: 64MB; ints are 4 bytes. 8-)
RichieHindle
Whoops, wasn't paying attention to the "int" part.
bkritzer
+10  A: 

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.

wrang-wrang
+1, beat me to it! Using malloc or new[] to allocate the memory from the heap would also fix the problem, if that's indeed the problem.
Jim Lewis
Attempt to eat 64MB from stack is most likely a problem, indeed, +1.
Andrew Y
A: 
bill weaver