Re: Static Variables



akhil.misra@xxxxxxxxx wrote:
where does a static variable inside a function get stored?? where does
it go in memory?? like Global goes in RAM, Local variable goes on
Stack... but how does static variable takes place in memory??

The C Standard does not impose any specific constraints for the
location of objects. As long as they meet the properties specified in
their declaration, the compiler is free to place them wherever it
wants.

Static objects are essentially a compromise between unqualified file
scope objects, (global), on the one hand and unqualified block scope
objects, (local), on the other hand. While they persist throughout the
program's lifetime, they're accessible only from their scope, (unless
a pointer is used). That implies that it's pretty much out of question
to place them on a stack, (except if it's declared within the main
function).

<OT>
In most implementation statics and other file scope objects are
usually placed in a section of memory called static storage. String
literals might also be placed here, or they might be placed in a read-
only area of storage. But such details are implementation specific,
you should not rely on them if you want your code to be portable. In
most cases the location of objects is quite unneccessary to know.
</OT>

.



Relevant Pages

  • Re: Are static array pointers thread safe?
    ... 'static' that it is NOT allocated on the stack! ... how allocation works; I've even had one correspondent assure me that he would not use ... globals, static data members of classes, and local statics. ... observe all sorts of strange things in the absence of synchronization, ...
    (microsoft.public.vc.mfc)
  • Re: Are static array pointers thread safe?
    ... array. ... a separate area of memory dedicated to them. ... globals, static data members of classes, and local statics. ... observe all sorts of strange things in the absence of synchronization, ...
    (microsoft.public.vc.mfc)
  • Re: storage locations for different data types
    ... the memory for all objects with static storage duration ... This is static storage. ... that const statics CAN be treated differently from non-const statics. ...
    (comp.lang.cpp)
  • Re: Use of static and extern
    ... >will reside in memory until the program terminates? ... forms of global object use memory allocated at program start-up. ... The reason that function scope statics provide functions with state ...
    (alt.comp.lang.learn.c-cpp)
  • Re: variable allocated from stack/bss ??
    ... allocated from bss the default value would be 0 ... auto respectively instead of bss or stack. ... statics with extra scope.) ... The reality is that auto variables are commonly mapped to ...
    (comp.lang.c)

Loading