Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4256952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:25:28+00:00 2026-05-21T05:25:28+00:00

I had a discussion this morning with a colleague about static variable initialization order.

  • 0

I had a discussion this morning with a colleague about static variable initialization order. He mentioned the Nifty/Schwarz counter and I’m (sort of) puzzled. I understand how it works, but I’m not sure if this is, technically speaking, standard compliant.

Suppose the 3 following files (the first two are copy-pasta’d from More C++ Idioms):


//Stream.hpp
class StreamInitializer;

class Stream {
   friend class StreamInitializer;
 public:
   Stream () {
   // Constructor must be called before use.
   }
};
static class StreamInitializer {
  public:
    StreamInitializer ();
    ~StreamInitializer ();
} initializer; //Note object here in the header.

//Stream.cpp
static int nifty_counter = 0; 
// The counter is initialized at load-time i.e.,
// before any of the static objects are initialized.
StreamInitializer::StreamInitializer ()
{
  if (0 == nifty_counter++)
  {
    // Initialize Stream object's static members.
  }
}
StreamInitializer::~StreamInitializer ()
{
  if (0 == --nifty_counter)
  {
    // Clean-up.
  }
}

// Program.cpp
#include "Stream.hpp" // initializer increments "nifty_counter" from 0 to 1.

// Rest of code...
int main ( int, char ** ) { ... }

… and here lies the problem! There are two static variables:

  1. “nifty_counter” in Stream.cpp; and
  2. “initializer” in Program.cpp.

Since the two variables happen to be in two different compilation units, there is no (AFAIK) official guarantee that nifty_counter is initialized to 0 before initializer‘s constructor is called.

I can think of two quick solutions as two why this “works”:

  1. modern compilers are smart enough to resolve the dependency between the two variables and place the code in the appropriate order in the executable file (highly unlikely);
  2. nifty_counter is actually initialized at “load-time” like the article says and its value is already placed in the “data segment” in the executable file, so it is always initialized “before any code is run” (highly likely).

Both of these seem to me like they depend on some unofficial, yet possible implementation. Is this standard compliant or is this just “so likely to work” that we shouldn’t worry about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:25:29+00:00Added an answer on May 21, 2026 at 5:25 am

    I believe it’s guaranteed to work. According to the standard ($3.6.2/1): “Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.”

    Since nifty_counter has static storage duration, it gets initialized before initializer is created, regardless of distribution across translation units.

    Edit: After rereading the section in question, and considering input from @Tadeusz Kopec’s comment, I’m less certain about whether it’s well defined as it stands right now, but it is quite trivial to ensure that it is well-defined: remove the initialization from the definition of nifty_counter, so it looks like:

    static int nifty_counter;
    

    Since it has static storage duration, it will be zero-initialized, even without specifying an intializer — and removing the initializer removes any doubt about any other initialization taking place after the zero-initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a discussion with a colleague at work, it was about SQL queries
Today I had a discussion with a colleague about nested functions in Javascript: function
A while ago, I had a discussion with a colleague about how to insert
We had a meeting this morning about how would should store our ID for
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
we had a heated discussion about a method name. We have a class User
At work we had a discussion about adding a fulltext search engine to our
I had a discussion recently about looking for a method to generate truly random
I had a discussion with a colleague today around using query strings in REST
Today at work I had a discussion with my co-workers and my boss about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.