Datta said:
Could you plz tell me where exactly local , global , static variables mapped
in memory layout ?
On Unix systems a program is contained in a process. The process is a
single thread of execution and a virtual memory space. The typical
runtime environment will place the program code at the low end of the
memory space, followed by static variables (which are part of the image
that is loaded from disk --- it is how they get initialized). After
this is the heap which builds up. The environmental varables are at the
high end of the memory space and just beneath them is a stack which
builds down. (Like I said this is typical, it could vary from system to
system.)
All malloc() calls add blocks of memory to the top of the heap. free()
releases blocks of memory from the heap. Note that free() could release
a block in the middle of the heap creating a hole (fragmentation). All
function calls add stack frames to the stack which builds down. When
the two meet, you are out of memory, and you may have to try to compact
the heap (garbage collection).
Every time a function is called a new stack frame is created on the
stack. Local variables are stored in the stack frame of the
corresponding function. This allows for re-entrent and recursive
functions. When a function returns, the stack frame is deallocated and
the local variables in it distroyed. So local (or as C calls them
"automatic") variables only live as long as the function does.
Static variables are not stored on the stack so they live between
function calls. The term "global" in C only referes to scope. All
global variables are static.
By default any variable decleared in a function is of storage class
"automatic" You can make this explicit by using the keyword "auto" but
since it is the default, nobody bothers.
If you qualify a variable definition with the keyword "static" inside of
a function, that variable is a static variable (not stored in the
stack frame) but its scope is restricted to the function that defined it
(no other function can refer to this variable, it is private to the
function which decleared it).
By default, if you define a variable outside of a function body its
storage class is static. It will also have global scope. Any function
can see it. A .c file, and all of its "#include"s is called a
"translation unit" A translation unit produces an object file which is
linked by the linker into an executable. If translation unit "A"
defines a variable outside of a function body, that variable will be
static. If translation unit "B" declears the same variable with an
"extern" declearation, then when the two object files are linked, the
code in translation unit "B" will manipulate the variable defined in
translation unit "A" (note the use of the words "define" and "declear").
If translation unit "A" qualifies the variable defination with the
keyword "static", then the variable will have a static storage class,
but its scope will be restricted to the current translation unit. That
is, the symbol is not published to the linker and other translation
units will not be able to access it. The same is true for function
definations which are qualified by the keyword "static" (they remain
local to the translation unit). A translation unit is what some other
languages call a "module."
You should read, "The C Programming Language" by Kernighan & Ritchie.
All of this is in there. O'Reilly's "C Pocket Refrence" is also
surprisingly good, and more update (includes the C99 extentions).