-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
anonymous wrote:
|>Static variables are declared in neither heap nor stack, they are declared
|>in C source code files. And they are *allocated* whereever the
|>implementor chooses to, typically neither in heap (the malloc and friends
|>arena) nor in stack (automatic allocation arena and function call related
|>data).
|
|
| But is it not required to allocate static variables in heap,
Nope. The C language doesn't even recognize the concept of 'heap', let alone
require static variables to be allocated in it. However, a specific C compiler
/might/ implement the language such that static variables are allocated in the
'heap', if such a thing exists in the implementation platform.
For that matter, it is perfectly concievable (although hardly likely) that a C
compiler will allocate static variables in a 'file', or even in a 'database'.
There are no restrictions on the mechanism that the compiler uses to allocate
the variables, so long as the allocation and access mechanisms do not conflict
with the requirements of the C language.
| so
| that their values are intact even after a function is called more than once?
Static variables are guaranteed (by the C language) to retain their values
across function calls. How the compiler manages that is entirely up to the
compiler.
| Then generally where are they allocated?.
OK, lets step away from the theory for a second. There's only so many times we
can say that the /mechanism/ isn't something that we know about or care about,
so long as the C language /policy/ is adhered to.
In practice, compilers don't use /either/ 'heap' or 'stack' to store static
variables. Both 'heap' and 'stack' are typically /dynamic/ allocation
mechanisms, and since we're talking about static data, these mechanisms are
unwieldly to use in that fashion.
C compilers (at least of the Unix kind) typically store static variables in
specific, pre-allocated areas of the BSS or DATA segments of the program's
memory map. They typically allocate the program's data area (in the resulting
'load module') such that it looks like...
~ | constants & initialized statics | uninitialized statics | heap | stack |
~ +=================================+-----------------------+......'.......+
~ | populated at pgm compile time | populated at program execution time |
~ \ /
~ bottom of memory top of memory
The constants and initialized statics consist of those data elements that
a) are of global scope and have been given initializers in the source code, or
b) are of local scope, defined as "static", and have been given initializers
~ in the source code, or
c) are string constants, or other truely static data
The uninitialized statics consist of those data elements that
a) are of global scope which have not been given initializers in the source
~ code, or
b) are of local scope, defined as "static", but have not been given
~ initializers in the source code,
The 'heap' and 'stack' are just whatever's left over after all the statics are
loaded into memory. It's just one big scratchpad area that gets managed by the
C runtime. Function calls to malloc() typically reserve blocks of memory
starting at the 'heap' end of this scratchpad area and build towards the
'stack' end. The C runtime allocates automatic variables starting at the top
of the 'stack' end, working down toward the 'heap' end.
So, you see, in theory, there is no 'heap'. In practice, statics aren't
allocated in the 'heap', if there is one.
Does this make it any clearer?
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (
http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird -
http://enigmail.mozdev.org
iD8DBQFAsLZ+agVFX4UWr64RAvoxAJ4zzpq0X+5dQeoUqnZ64b4iuom7WACfYj0o
hiR8Knqi9tA+ZFBUeUwh0Ys=
=BjYz
-----END PGP SIGNATURE-----