DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Static Initialization Fiasco - On purpose

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    Static Initialization Fiasco - On purpose

    Just for fun, I was investigating the order of dynamic initialization of static objects.
    In a file name t.h, I put
    Code:
    struct T {
       static std::vector<std::string> me;
       static int add(std::string s) { me.push_back(s); return me.size(); }
    };
    (Plus needed headers for vector and string.)
    "std::vector<std::string> T::me" is in t.cpp.
    The file main.cpp prints out the values in T::me:
    Code:
    #include "t.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        T::me.push_back("main");
        cout << "T::me.size()=" << T::me.size() << endl;
        for (unsigned i = 0; i<T::me.size(); ++i) {
            cout << i << "-" << T::me[i] << endl;
        }
        return 0;
    }
    next, I create "a.cpp" and put the following in it:
    Code:
    #include "t.h"
    
    int a = T::add("a");
    Do the similar for file b.cpp and c.cpp using "b" and "c" as appropriate.
    Compile using g++ *.cpp, then run ./a.out. The order of static initialization from compilation unit to compilation unit is unspecified. In my case it is consistently in reverse alphabetical order. I get:
    3 - c
    2 - b
    1 - a
    0 - main

    No problems so far.

    Now I create u.cpp like a.cpp but using "u". Recompile/rerun, "u" does not show up in the list.

    Is it because I never referenced u? I never referenced a,b,c, but I change main:
    Code:
    #include "t.h"
    #include <iostream>
    using namespace std;
    
    extern int u;
    
    int main()
    {
        cout << "u=" << u << endl;
        T::me.push_back("main");
        cout << "T::me.size()=" << T::me.size() << endl;
        for (unsigned i = 0; i<T::me.size(); ++i) {
            cout << i << "-" << T::me[i] << endl;
        }
        return 0;
    }
    The program prints out "u=2", but "u" is not in the list. Shouldn't u have been dynamically initialized *before* use and, therefore, T::me have been updated to include "u"? I think there should be a better explanation than that u comes after t in the alphabet.

  2. #2
    Join Date
    Oct 2007
    Posts
    369
    I've got it. Simple, really.
    T::me is zero initialized statically according to the rules of C++. There is a constructor, however, to run dynamically. There's no guarantee when that constructor runs. It is apparently - in this case - running after u is initialized.

Similar Threads

  1. Replies: 4
    Last Post: 08-18-2007, 11:29 AM
  2. TicTacToe help...
    By MP35719 in forum Java
    Replies: 0
    Last Post: 12-16-2006, 07:06 PM
  3. KeyListener not responding
    By lordanki in forum Java
    Replies: 3
    Last Post: 04-04-2006, 10:13 AM
  4. Trouble buffering transparent gif
    By axlan in forum Java
    Replies: 2
    Last Post: 06-22-2005, 08:21 AM
  5. Replies: 2
    Last Post: 05-24-2005, 06:56 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