Link to home
Start Free TrialLog in
Avatar of KayodeCS_BU
KayodeCS_BU

asked on

static memory in a C program

I am a little confused about the memory management of a C program.  When a C program is loaded into main memory, do statically initialized variables get loaded into the initialized data segment, or onto the stack.
For example if I have the program,
int globx = 1;
int y;
int main (){
static int x = 10;
return 0;
}
I know globx is in the initialized data segment, but is x as well?
Also, do you know where y would get put?  I have the understanding that y would not be in object file.  If you have a good understanding of how memory is arranged and how it is loaded into memory could you please clear this up for me.
Thank you,
K
Avatar of stehan
stehan

Hi !

I don't know what you mean with 'initialized data segment'.

This is what I do know:

globx and y are global variables, they are known to all functions you'll create in your program. Space will be reserved on the stack until the program is finished.

Defining x as a static in the function main() means that it is a local variable whose value will not be changed if you pass it to a function (even by reference).

If you had defined x as static outside main(), lets say directly after int y, it would have become a variable only known to functions in the same file, to which it would look as a global variable. But functions defined in a different file would not know it.

Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of sarda_ramesh
sarda_ramesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KayodeCS_BU

ASKER

Thank you, I think I got it now.  The static variable x would be stored in the initialized data segment.
There is no such concept called initialized data segment. When you compile the code fragment the compiler is going to allocate the memory in two different ways:

1. For globalx, and y , compiler is going to allocate memory for them in data segment.
2. and for others, in the function, the memory will be alloocated during run time within the stack (your int x).

Since you are providing the value of globalx, it will be initialized by the compiler itself. But there is no difference between the allocation of globalx and y.

Hope this may help you.
There is no such concept called initialized data segment. When you compile the code fragment the compiler is going to allocate the memory in two different ways:

1. For globalx, and y , compiler is going to allocate memory for them in data segment.
2. and for others, in the function, the memory will be alloocated during run time within the stack (your int x).

Since you are providing the value of globalx, it will be initialized by the compiler itself. But there is no difference between the allocation of globalx and y.

Hope this may help you.

Give further comments...
I'm a bit out of touch, but I believe they are loaded into the initialised data segment, and the order they occur in may depend on initialisation (initialised first, unintialised later ???). Seem to remember a description of this in petzold's original windows programming book but I could be way off