global/local static object lifetimes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: global/local static object lifetimes

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    global/local static object lifetimes

    I need to clarify my understanding of the lifetime of certain static objects.
    Consider the following file, Experiment.h :
    Code:
    namespace mine
    {
    
        static int persist_ns;
    
        class Experiment {
            static int persist_Experiment;
            ...
        }
    }
    Further suppose that this file is *not* included by the file that defines main().

    If this is compiled into a program, mine::persist_ns is created before the main() is entered. (Correct?)

    Is persist_Experiment also a "global static"; i.e., created before main is executed?

    My "C++ in a nutshell" says, "Local static objects are constructed when the execution reaches the object's declaration."
    I'm thinking that this refers to a case like this:
    Code:
    int fun() {
       static int my_static_int;
       ...
    In this case, my_static_int is created the first time fun() is called. Correct?

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    the class static is the same as a global static I think, since a static in a class seems to be shared amongst all class instances (or, maybe I just set mine up this way?). So whenever they are created, it is at the same time, the global scope, which is probably before main but I do not know if that is true across all platforms.

    The function one is created the first time the function is called, as you said, but it is not created on the normal function call variable area (usually, just the function call stack is used or is that only windows? ) but is created in the same area globals are.

    I am not 100% sure how much of where a variable is stored is set in stone and how much of it just happens to be similar across platforms just due to it being a good approach.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    It's more complicated than that. First, you need to define what you mean by "created". Objects with static storage duration (globals, namespace scope and class members which are declared static) undergo two phases of construction: first they are statically initialized which in simple words means: zero initialized. Next, they undergo dynamic initialization during which the initializers (constructors, assuming we're dealing with class objects) are invoked. So you could accidentally access a half-baked object which has undergone the first phase but not the second phase. The only way to guarantee that your objects are fully-constructed before you access them from a different translation unit is to wrap each such object in a function. Alternatively, make them global and move them to the translation unit which declares main (but again, other translation unit might access them prematurely).
    You can find all the details here: http://www.informit.com/guides/conte...lus&seqNum=212
    As far as static class members of pod types are concerned: they are pretty much identical to global objects in the sense that they can be visible from other translation units.
    Last edited by Danny; 04-20-2009 at 11:20 PM.
    Danny Kalev

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    well, the fundamental question is not POD or non-POD, but weather an object with static static storage duration requires dynamic initialization. for example:
    Code:
    #include <iostream>
    
    extern int first ;
    
    int second = ( ( std::cout << "initializing second with first(" << first << ")\n" ), first ) ;
    
    int first = ( ( std::cout << "initializing first\n" ), 20 ) ;
    
    int main()
    {
       std::cout << "first: " << first << "\tsecond: " << second << '\n' ;
    }
    second will be initialized before first (in order of definition), with the uninitialized (zero initialized) value of first.

    another resource dealing with initialization order across translation units: http://www.parashift.com/c++-faq-lit...html#faq-10.12

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    you're right, dynamic initialization may also apply to POD types, e.g., those that are initialized with a non-constant expression that the compiler can't evaluate.
    The relative order of initialization is guaranteed within the translation unit but there's no such guarantee across translation units.
    Danny Kalev

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  2. Replies: 0
    Last Post: 05-27-2003, 12:12 PM
  3. Top fixed screen
    By Cheng in forum Web
    Replies: 1
    Last Post: 11-06-2001, 10:43 AM
  4. Replies: 13
    Last Post: 10-03-2001, 05:05 PM
  5. private and static
    By Mark in forum Java
    Replies: 3
    Last Post: 06-09-2000, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center