Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the “static initialization order fiasco”?

0
Posted

Whats the “static initialization order fiasco”?

0

[Recently created (on 9/97). Click here to go to the next FAQ in the “chain” of recent changes.] A subtle way to kill your project. The static initialization order fiasco is a very subtle and commonly misunderstood aspect of C++. Unfortunately it’s very hard to detect — the errors occur before main() begins. In short, suppose you have two static objects x and y which exist in separate source files, say x.cpp and y.cpp. Suppose further that the constructor for the y object calls some method on the x object. That’s it. It’s that simple. The tragedy is that you have a 50%-50% chance of dying. If the compilation unit for x.cpp happens to get initialized first, all is well. But if the compilation unit for y.cpp get initialized first, then y’s constructor will get run before x’s constructor, and you’re toast. I.e., y’s constructor will call a method on the x object, yet the x object hasn’t yet been constructed. I hear they’re hiring down at McDonalds. Enjoy your new job flipping burgers. If

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123