You're blowing out your stack which is typically a limited resource, in your case not enough to hold the nine or so megabytes your array takes up.
There are a couple of possible solutions. If you only need one copy, you can declare it outside the function (or as a static within the function) - this will generally keep it off the stack and put it into an area with less limited storage.
Alternatively, you can allocate it dynamically, and ensure you manage it properly.
You may also be able to configure a larger stack size depending on the environment.
By way of example, the following bash script will work out how much data you can allocate on the stack in your (default) environment:
((sz = 0)) ((delta = 1000000)) while [[ ${delta} -gt 0 ]]; do while true; do sleep 1 ((sz += delta)) printf "Trying ${sz} ... " rm -f qq.c echo "#include <stdio.h>" >>qq.c echo "int main (void) {" >>qq.c echo " char buff[${sz}];" >>qq.c echo " puts (\"hello\");" >>qq.c echo " return 0;" >>qq.c echo "}" >>qq.c gcc -o qq qq.c trap 'echo "segfault!"' CHLD rm -f flagfile ( bash -c './qq >flagfile 2>/dev/null' ) >/dev/null 2>&1 if [[ "$(cat flagfile)" == "hello" ]]; then echo "okay" else echo "crashed" ((sz -= delta)) break fi done ((delta = delta / 10)) done
It works by constructing a C program of the form:
#include <stdio.h> int main (void) { char buff[XYZZY]; puts ("hello"); return 0; }
where XYZZY is gradually changed to see where the program first crashes from lack of stack space. The output in my environment (CygWin) goes something like this:
Trying 1000000 ... okay Trying 2000000 ... okay Trying 3000000 ... crashed Trying 2100000 ... crashed Trying 2010000 ... okay Trying 2020000 ... okay Trying 2030000 ... okay Trying 2040000 ... okay Trying 2050000 ... okay Trying 2060000 ... okay Trying 2070000 ... okay Trying 2080000 ... crashed Trying 2071000 ... crashed Trying 2070100 ... crashed Trying 2070010 ... okay Trying 2070020 ... okay Trying 2070030 ... okay Trying 2070040 ... okay Trying 2070050 ... okay Trying 2070060 ... okay Trying 2070070 ... crashed Trying 2070061 ... okay Trying 2070062 ... okay Trying 2070063 ... okay Trying 2070064 ... okay Trying 2070065 ... crashed
meaning that two megabytes is about where it maxes out.