[Solved] Segmentation fault 11 because of a 40 MB array in C


Because you’re creating that as an automatic variable, it will go on the stack. The stack size is not very large. A general rule of thumb is for any objects larger than a few KBs, always dynamically allocate them on the heap using malloc() (or new for C++).

Your program is crashing because the size of the stack grew larger than it was allowed to. That’s known as a stack overflow and is usually seen when you have unbounded recursion.

The size of the stack is implementation defined, so it’s very possible that the default stack size on Linux is larger than on OS X.

9

solved Segmentation fault 11 because of a 40 MB array in C