Stacks and C go well together. Run out of stack space, and your program will die. Normally this occurs because of things like deep recursion, or large allocations of stack data. For example, trying to store a large static array in C. On OSX, most stack sizes default to 8MB. So if an int is 4 bytes in size, then 2,000,000 of them can be stored in this space. Quite a lot of data. But when we try to store an image of size 2048×2048 = 4,194,304 pieces of 4 bytes data = 16,777,216 bytes – WAY TOO MUCH. This has to be stored dynamically.
To find the size of a stack, it is possible to use utilities such as limit (csh, tcsh) or ulimit -a (sh, bash):
>ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited max locked memory (kbytes, -l) unlimited max memory size (kbytes, -m) unlimited open files (-n) 2560 pipe size (512 bytes, -p) 1 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 709 virtual memory (kbytes, -v) unlimited
Is it possible to increase the size of a stack? The answer is yes. For gcc, increasing the size of the stack to 16MB (0x1000000), can be done in the following manner:
gcc -Wl,-stack_size -Wl,1000000 needLargerStack.c
It just means that when you create a structure in C, you have to be mindful of the size of that structure.