Above the text segment is the static data segment. (static refers to variables that are declared to be of a static size during the compile) Historically this comes in two blocks - initialized data and uninitialized data. Both contain the addresses for variables defined outside of any function. (This reflects the C programming language, by the way) So a line like ...
int a = 10;would create a spot for variable a in the initialized data segment. A line like ...
int a[100];would create a spot for 100 integers in the uninitialized data segment. This data may (or may not) be initialized when the program begins execution.
Above the static memory segment is the dynamic memory segment, or heap. If you need to declare a block of memory of a size that is determined at run time, you will probably use some form of the "malloc" function when programming in C. The heap grows upward toward the stack.
Above the heap is the stack. The stack "is where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to [sic], and certain information about the caller's environment (such as some of the machine registers) is saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. By utilizing a stack in this fashion, C functions can be recursive." (Stevens 167-168)
The final segment is where your environment variables are stored. These are things like "DISPLAY" or "PRINTER" or "PATH". You can see them all with the "env" command. Also stored here are the command line arguments - things you pass to the program from the command line. Usually these are referred to as the "argv[]" variables. There are argc of them.
Ok, so suppose you get a seg fault in a highly recursive
program. Using your debugger you discover the memory problem to be a
line that just should not be having a memory problem. Diagnosis. The
stack ran into the heap and things are just not going to work. What
can you do?
ulimit -SsThe default limit on Solaris is only 8k.
You can reset the stack size (if you have root permission) to anything less than the hardware limit. To find out the hardware limit ...
ulimit -HsTo change the current stack size:
ulimit -Ss sizeThe size has to be a multiple of eight, or maybe only four.
You can test your stack setting with the following program. If you double your stack size, say from 8192 to 16384 you should notice the count of the program will roughly double. I wouldn't run this program with a stack much larger than 16384 as it may take a while before you get the seg fault.
void a(int c)
{
c++;
printf("c=%d\n",c);
a(c);
}
main ()
{
a(0);
}
On Linux systems the default limit of the stack is supposedly 2MB,
but my installations did not have the ulimit command.
Return to Gene's Home Page
Return to Gene's Random Unix Crap