Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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
0
KayodeCS_BU
Asked:
KayodeCS_BU
1 Solution
 
stehanCommented:
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.
0
 
sarda_rameshCommented:
In C memory management deals basically with two types:

1. On Stack
2. On Heap.

http://www.cs.huji.ac.il/~plab/lectures/Class3.pdf

check this out .. it will probably answer your queries and clear your concepts

regards
ramesh
0
 
KayodeCS_BUAuthor Commented:
Thank you, I think I got it now.  The static variable x would be stored in the initialized data segment.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rajeev_devinCommented:
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.
0
 
rajeev_devinCommented:
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...
0
 
WaffleSouffleCommented:
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now