PDA

View Full Version : [SOLVED] The Memory Layout of a C program



PanP5
April 12th, 2010, 03:56 PM
A C program is composed of the following pieces:


The Text segment

The Initialized Data Segment

The Uninitialized Data Segment

The Stack

The Heap

The initialized and uninitialized data segment... do those strictly contain the variables declared/initialized in the main function? Is everything declared/initialized inside another function simply stored on the stack?

For example:


int foo()
{
int j = 2;
return j;
}

int main(int argc, char *argv[])
{
int k;
int i = 5;
k = foo();

return 0;
}

Where in the program memory do i,j,and k reside? When does that happen?

matthew.ball
April 12th, 2010, 04:03 PM
Stack (http://en.wikipedia.org/wiki/Stack_(data_structure)).

Heap for Dynamic memory allocation (http://en.wikipedia.org/wiki/Dynamic_memory_allocation).

MadCow108
April 12th, 2010, 04:25 PM
in your example it all lies in the stack.
the un/initialized segments are for global variables
The stack and heap are dynamic and grow/shrink as the program progresses. the other segments are constant in size


static int i = 5; //should be in rw initialized segment
static const char * c = "5"; //should be in read-only initialized segment
static int j; //should be in uninitialized segment (but initialized to zero)

int main(...) { // machine instructions are in text segment
int a;
int b; // both on stack (frame of main)
char * c = malloc(4); // pointer is on stack, memory allocated on heap
}

int f(void) {
int a; // stack (frame of f)
}

PanP5
April 12th, 2010, 06:55 PM
in your example it all lies in the stack.
the un/initialized segments are for global variables

Thanks! That was exactly what I was unclear about.