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

  • SEARCH
  • Home
  • 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 8199901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:15:58+00:00 2026-06-07T06:15:58+00:00

In C++, what is the expected runtime cost in a reasonable compiler of initializing

  • 0

In C++, what is the expected runtime cost in a reasonable compiler of initializing a static variable with a variable value as opposed to a constant value?

For example consider this code:

bool foo();
bool baz1() {
  const bool value = foo();
  static bool alternate1 = value;
  static bool alternate2 = false;

  // Do something.
  return alternate1;
}

What is the expected run-time cost difference between alternate1 and alternate2?

  • 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-06-07T06:15:59+00:00Added an answer on June 7, 2026 at 6:15 am

    Initialisation from a compile-time constant (alternate 2) will most likely happen during program startup, with no cost each time the function is called.

    Thread-safe initialisation of a local static variable will cause the compiler to generate something like this pseudocode:

    static bool alternate1;
    
    static bool __initialised = false;
    static __lock_type __lock;
    if (!__initialised) {
        acquire(__lock);
        if (!__initialised) {
            alternate1 = value;
            __initialised = true;
        }
        release(__lock);
    }
    

    So there is likely to be a test of a flag each time the function is called (perhaps involving a memory barrier or other synchronisation primitive), and a further cost of acquiring and releasing a lock the first time.

    Note that in your code, foo() is called every time, whether or not the variable is initialised yet. It would only be called the first time, if you changed the initialisation to

    static bool alternate1 = foo();
    

    The details are implementation-dependent of course; this is based on my observations of the code produced by GCC.

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

Sidebar

Related Questions

I'm getting a Microsoft JScript runtime error: Object expected on this line of code:
This code compiles and works as expected (it throws at runtime, but never mind):
Getting Expected ',' or '{' but found '#44559' error. My code looks like this:
This is to be expected but I can't seem to find a runtime that
This script is getting Microsoft JScript runtime error: Object expected on If (out_params.bGranted) line.
I expected this to print [b] but it prints [] : $x = abc;
As expected, the compiler (VisualStudio 2008) will give a warning warning C4715: 'doSomethingWith' :
We get expected : end of statement error for the below code and VB
The Jquery script that controls my tabcontainer gives an object expected runtime error. I
Why I got this error Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback

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.