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 4242310
In Process

The Archive Base Latest Questions

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

I noticed that if you initialize a static variable in C++ in code, the

  • 0

I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function.

That is cool, but how is that implemented? Does it translate to some kind of twisted if statement? (if given a value, then ..)

void go( int x )
{
    static int j = x ;
    cout << ++j << endl ; // see 6, 7, 8
} 

int main()
{
    go( 5 ) ;
    go( 5 ) ;
    go( 5 ) ; 
}
  • 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-21T03:25:31+00:00Added an answer on May 21, 2026 at 3:25 am

    Yes, it does normally translate into an implicit if statement with an internal boolean flag. So, in the most basic implementation your declaration normally translates into something like

    void go( int x ) {
      static int j;
      static bool j_initialized;
    
      if (!j_initialized) {
        j = x;
        j_initialized = true;
      }
    
      ...
    } 
    

    On top of that, if your static object has a non-trivial destructor, the language has to obey another rule: such static objects have to be destructed in the reverse order of their construction. Since the construction order is only known at run-time, the destruction order becomes defined at run-time as well. So, every time you construct a local static object with non-trivial destructor, the program has to register it in some kind of linear container, which it will later use to destruct these objects in proper order.

    Needless to say, the actual details depend on implementation.


    It is worth adding that when it comes to static objects of “primitive” types (like int in your example) initialized with compile-time constants, the compiler is free to initialize that object at startup. You will never notice the difference. However, if you take a more complicated example with a “non-primitive” object

    void go( int x ) {
      static std::string s = "Hello World!";
      ...
    

    then the above approach with if is what you should expect to find in the generated code even when the object is initialized with a compile-time constant.

    In your case the initializer is not known at compile time, which means that the compiler has to delay the initialization and use that implicit if.

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

Sidebar

Related Questions

I noticed some code of a colleague today that initialized class variables in the
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I noticed that the generic IEnumerator<T> inherits from IDisposable, but the non-generic interface IEnumerator
This is another debated subject, but this time i am searching only for the
I noticed that tail +2 is supported in Solaris ksh, but in Red Hat
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that my Designer.vb file of one of my forms has a lot
We noticed that lots of bugs in our software developed in C# (or Java)
i noticed that paypal displays a very different favicon, one that's not just a
I noticed that there is one change about ASP.NET Routing. I cannot understand why

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.