Here is the scenario:
I am declaring an array and NOT USING IT ANY WHERE IN THE PROGRAM yet. If I declare it to be of length 100 or less, it works fine. For greater length, it gives segmentation fault. I can’t understand this behavior.
ATL_RAM_BUFFER_L3 an_unknown_array[128];
Where ATL_RAM_BUFFER_L3 is a structure of almost 72KB.
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
bashscript will work out how much data you can allocate on the stack in your (default) environment:It works by constructing a C program of the form:
where
XYZZYis gradually changed to see where the program first crashes from lack of stack space. The output in my environment (CygWin) goes something like this:meaning that two megabytes is about where it maxes out.