views:

86

answers:

3

Hi ! I am a newbie to C++ and facing a problem.

I read in a book that memory is allotted to a static variable, once the object is created of that class. Now, what if I make this static variable global ? When would be it initialized in that case ?

Plus, I have also read in some articles that static variables are allotted on heap and they dont rely on the construction of objects...is this true ? If yes, please explain the memory initialization steps to me, I need help.

Thanks a lot !

+4  A: 

Static variables are not at all stored in Heap.

Uninitialized static variables are stored in BSS segment.

Initialized static variable are stored in Data Segment.

You can read this article which explains all of it in a great way: http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

bits
Is the standard that specific on global variables are stored? If so I would like a reference from the standard.
Martin York
A: 

On programstart (before main) memory will be allocated for your static global variable.

I have no idea for the 2nd question.

Quonux
+5  A: 

First: STOP THINKING ABOUT GLOBAL VARIABLES IN C AND C++, or you will perpetuate your state of confusion. The issue is more complex than in, e.g., Python or Pascal, and therefore you can't just use a single word for the concept(s).

Second, there is no "heap" or "stack" -- these are operating system and CPU details, and have nothing to do with the abstract C++ language specification.

Now, variables have 1) scope, 2) linkage and 3) storage class. The static keyword is used to affect the all three aspects, depending on where you use it.

Scope: where the variable is declared. If within a function, it is a function-scope, if outside of a function, it is file-scope (what you probably refer to as "global variable").

Linkage: whether the variable is accessible from other compilation units (relevant when your program consists of multiple source files).

Storage class:

Static variables are allocated in implementation-defined manner upon program startup, and they live until the program ends. They cannot be "freed" or "reallocated". (a typical implementation are BSS and DATA segments as others have mentioned).

Automatic variables exist only at function scope, they are allocated (and possibly initialized) on function entry (usually on the CPU's stack) and deallocated on function exit.

Dynamic storage class is what you probably refer to "the heap". Those are variables whose storage is directly manipulated through malloc/free or new/delete. Storage for static variables is managed in a VERY different way than dynamic storage, and the two kinds of storage are fundamentally incompatible.

Example:

===
// ALL file-scope variables have static storage class
int global1;        // file-scope, external linkage
static int global2; // file-scope, internal linkage

void f()
{
  static int x;     // static storage class, function-scope
  int y;            // automatic storage class, function-scope

  free(&x);         // ILLEGAL, WILL CRASH!
  free(&y);         // DITTO!
  free(&global1);   // DITTO!
}
===

Now, all variables with static storage class (global1, global2 and x) are allocated and initialized before program startup. If you do not specify initial values, they are default-initialized in UNSPECIFIED ORDER. (For primitive types, this just means filling with zeros).

Static variables are deallocated only at program exit. This means, that 'x' in function f will be initialized ONLY ONCE (at program startup) and that it will RETAIN ITS VALUE between invocations of the function (as opposed to y which will be allocated on each function entry and deallocated at each function exit, thus also its value destroyed). Note that using static variables within functions is very incompatible with multithreading and reentrancy, unles you know very well what you're doing.

zvrba
Note: It is not totally unspecified. All file scope variables in the same compilation-unit are initialized in the order of declaration/definition. It is just not specified what the order is across compilation units (or even if file scope variables in different compilation units are interweaved during initialization).
Martin York
In the above program: 'global1' is initialized first with 0. 'global2' is initialized second with 0. 'x' is initialized the first time the function f() is called.
Martin York
I would like to add one more doubt here. What would be the difference between initialization of a static function and an ordinary function ? I have read that even the ordinary functions have only 1 copy whatever the number of objects may be - this is very similar to static functions...
Ishi
'static' on functions affects only linkage: the function gets internal linkage and is accessible only from the same file where it is defined. This makes it possible to have a static function named 'f' defined in multiple files. If you attempt to do that with non-static functions, the linker will complain about multiple definition of the symbol 'f'.
zvrba