static member variables

Discussion in 'C++' started by Rahul, Dec 1, 2007.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    It is known that static member variables can be accessed without
    creating an instance of the class and it serves as a global variable
    without polluting the global name space.

    So does that mean that the memory of the variable is allocated in
    BSS (data segment meant for static global variables) when the program
    is loaded and is available until the program is killed.

    Thanks in advance!!!
     
    Rahul, Dec 1, 2007
    #1
    1. Advertising

  2. Rahul

    Guest

    On 12ÔÂ1ÈÕ, ÏÂÎç8ʱ47·Ö, Rahul <> wrote:
    > Hi Everyone,
    >
    > It is known that static member variables can be accessed without
    > creating an instance of the class and it serves as a global variable
    > without polluting the global name space.
    >
    > So does that mean that the memory of the variable is allocated in
    > BSS (data segment meant for static global variables) when the program
    > is loaded and is available until the program is killed.
    >
    > Thanks in advance!!!


    The life cycle of static member variables is same as the program. But
    the order of them are not determinate. I thought that whether they are
    allocated in BSS depends on the compiler.
     
    , Dec 1, 2007
    #2
    1. Advertising

  3. Rahul

    Rolf Magnus Guest

    Rahul wrote:

    > Hi Everyone,
    >
    > It is known that static member variables can be accessed without
    > creating an instance of the class and it serves as a global variable
    > without polluting the global name space.
    >
    > So does that mean that the memory of the variable is allocated in
    > BSS (data segment meant for static global variables) when the program
    > is loaded and is available until the program is killed.


    The variable is destroyed after main() finished its execution. It has static
    storage duration, so it is handled like other variables with static storage
    duration (i.e. like global variables). How that is done is beyond the scope
    of the C++ standard. It's handled by the compiler. g++ usually puts them
    into the .bss or .data section depending on the type and initialization
    value.
     
    Rolf Magnus, Dec 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    543
    jeffc
    Nov 21, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    838
    Micah Cowan
    Jan 12, 2005
  3. aling
    Replies:
    6
    Views:
    506
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,399
    Pete Becker
    Dec 5, 2007
  5. Replies:
    9
    Views:
    1,008
Loading...

Share This Page