?
Solved

static memory in a C program

Posted on 2003-03-05
6
Medium Priority
?
198 Views
Last Modified: 2010-04-17
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
Comment
Question by:KayodeCS_BU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:stehan
ID: 8078063
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
 
LVL 1

Accepted Solution

by:
sarda_ramesh earned 1200 total points
ID: 8078182
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
 

Author Comment

by:KayodeCS_BU
ID: 8078994
Thank you, I think I got it now.  The static variable x would be stored in the initialized data segment.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8079044
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
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8079046
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
 

Expert Comment

by:WaffleSouffle
ID: 8081188
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Introduction to Processes
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question