When I try to initialize a 3D array of size 300*300*4 in a C program, my program stops running and reports stack overflow error. The system I am using has 3GB RAM, which should be sufficeint. Is there any way to increase memory allocated to a program? I am using Dev C++ on Windows Vista.
Either use malloc()
/free()
(or new[]
/delete[]
for C++), or a global array, or a local static array. If you try to create a non-static array within a function, it gets allocated on the stack and stacks are generally not very large.
You can also try initialising the array backwards; some OSs grow the stack dynamically as page faults occur, and since on x86 the stack grows numerically downwards, initialising backwards can help.
You need to increase how much stack space your program can use.
You can set the maximum stack size in the properties dialog under to "Linker | System | Stack Reserve Size"
Even if you have 3 GB of RAM, the stack size is limited by the OS and is typically small, as large structures like this are likely allocated on the heap, not the stack.
Since you're using C++, maybe you could use Boost?
boost::multi_array<int, 3> x (boost::extents[300][300][4]);
As you are using DevC++, presumably you are using the gcc compiler and via it the ld linker. The linker has an option -stack stacksize
which allows you to control the amount of memory allocated to the stack. How you would go about doing this from DevC++ I don't know.
I also don't know why anyone would use DevC++ at all - it is buggy and no longer being developed. You should consider changing to a better IDE - Code::Blocks has just had a new major release, is superior to DevC++ in every respect, and comes with a much more modern C++ compiler.