2

I'm having some problems initializing static string members in c++. I have several classes and each one is holding several static string members that represent an id. When I am initializing the variables by calling a static function everything is fine. However, when I'd like to assign one variable with the value of another it still holds empty string. What's problem with this code?

std::string A::id()
{
    std::stringstream sst; 
    sst << "id" << i;           
    i++;
    return sst.str();
}

  std::string B::str = A::id(); //prints "id0";
  std::string C::str = "str"; //prints str


  std::string D::str = B::str; //prints "" <-- what's wrong here?
  std::string D::str2 = C::str; //prints ""

It appears as if the variables I am referring to (B::str and C::str) havent been initialized yet. But I assume when D::str = B::str is executed C::str is initialized at the latest and therefore D::str should also hold the string "id0".

4
  • 5
    Welcome to static initalization hell :-)
    – Kerrek SB
    Sep 21, 2011 at 16:04
  • Do you initialize your attributes from a function, or outside any block ? Sep 21, 2011 at 16:05
  • Google "static initialization order fiasco"
    – Kaz Dragon
    Sep 21, 2011 at 16:05
  • Is everything you're showing in the same cpp?
    – MerickOWA
    Sep 21, 2011 at 16:06

2 Answers 2

10

This is Static Initialization Fiasco.

As per the C++ Standard the initialization order of objects with static storage duration is unspecified if they are declared in different Translation units.

Hence any code that relies on the order of initialization of such objects is bound to fail, and this problem is famously known as The Static Initialization Fiasco in C++.

Your code relies on the condition that Initialization of B::str and C::str happens before D::str, which is not guaranteed by the Standard. Since these 3 static storage duration objects reside in different translation units they might be initialized in Any order.

How to avoid it?

The solution is to use Construct On First Use Idiom,in short it means replacing the global object, with a global function, that returns the object by reference. The object returned by reference should be local static, Since static local objects are constructed the first time control flows over their declaration, the object will be created only on the first call and on every subsequent call the same object will be returned, thus simulating the behavior you need.


This should be an Interesting read:

How do I prevent the "static initialization order fiasco"?


4
  • 3
    Great link, but this would have been a much better answer if it summarized the info there. Sep 21, 2011 at 16:07
  • @MarkRansom: Done. But anyways the C++FAQ answers much more nicely.
    – Alok Save
    Sep 21, 2011 at 16:22
  • @Als thanks! I replaced them with static functions, now everything's working
    – Pedro
    Sep 21, 2011 at 16:23
  • @Als, nice save. You have my +1 now. I agree the C++FAQ answers the question nicely, but there are two reasons to have info here: first is that it gives you a reason to follow the link, second is that it guarantees the answer will remain useful even if the linked page is changed or removed. Sep 21, 2011 at 16:30
3

There is no guaranteed order of initialization for static variables. So don't rely on it. Instead init them with actual literal's, or better yet, init them at run-time when they are really needed.

1
  • +1 It's best to init them right at the beginning, before you've spawned any threads.
    – Praetorian
    Sep 21, 2011 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.