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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:33:59+00:00 2026-05-27T08:33:59+00:00

From C++ Primer, 5th Edition, page 407: All static duration variables share the following

  • 0

From “C++ Primer, 5th Edition”, page 407:

All static duration variables share the following two initialization
features:

  • An uninitialized static variable has all its bits set to 0.

  • A static variable can be initialized only with a constant expression.

A constant expression can use literal constants, const and enum
constants, and the sizeof operator. The following code fragment
illustrates these points:

int x;                        // x set to 0
int y = 49;                   // 49 is a constant expression
int z = 2 * sizeof(int) + 1;  // also a constant expression
int m = 2 * z;                // invalid, z not a constant
int main() {...}

My question is – WHY is this the standard? What is the (practical) reason for this.

What harm would have befallen us otherwise?

Especially, I find it hard to come up with a reason for the invalidity of:

int m = 2 * z; // invalid, z not a constant

since z itself is already known at compilation time.

Answer:

Simply put, according to the standard it is not guaranteed that instructions before the first statement of main will be done in order.
It is guaranteed though that all static storage will be zero-initialized before any other initialization.
This means that in the example I gave: int m = 2 * z; is an undefined behavior and m might either evaluate to 2*0=0 or it might be evaluated to `2*(2 * sizeof(int) + 1).

Out of the “C++ Standard – ANSI ISO IEC 14882 2003” 3.6.2 (p.44):

Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place.

…

An implementation is permitted to perform the initialization of an
object of namespace scope with static storage duration as a static
initialization even if such initialization is not required to be done
statically…

…

And the important bit:

As a consequence, if the initialization of an object obj1 refers to an
object obj2 of namespace scope with static storage duration
potentially requiring dynamic initialization and defined later in the
same translation unit, it is unspecified whether the value of obj2
used will be the value of the fully initialized obj2 (because obj2 was
statically initialized) or will be the value of obj2 merely
zero-initialized.`

Further reading about problems of this sort:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14

Edit: Put in answer. Pubby’s was the correct answer (and got accepted) but I really missed the “As a consequence..” part. Also, I think the link I added might be useful.

  • 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-27T08:33:59+00:00Added an answer on May 27, 2026 at 8:33 am

    Static initialization (technically constant initialization) requires constant expressions, but static storage duration variables do not.

    int x;                        // 0 initialized
    int y = 49;                   // statically initialized
    int z = 2 * sizeof(int) + 1;  // statically initialized
    int m = 2 * z;                // dynamically or statically initialized
    

    3.6.2 Together, zero-initialization and constant initialization are called static initialization; all other initialization is
    dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.

    Dynamic initialization can rely on functions and other things that can’t be decided at compile time. The compiler is allowed to perform static initialization if it can determine the result.

    The example you posted will work, although it can lead to trouble with more complicated initialization, as some variables can be initialized dynamically or statically:

    inline double fd() { return 1.0; }
    extern double d1;
    double d2 = d1; // unspecified:
    // may be statically initialized to 0.0 or
    // dynamically initialized to 1.0
    double d1 = fd(); // may be initialized statically to 1.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From C Primer Plus 5th Ed: Write a program that creates two eight-element arrays
From time to time I see an enum like the following: [Flags] public enum
Toward the end of Chapter 16 of the C++ Primer I encountered the following
I'd like to print out all prime numbers from an array with method. I
This is a great primer but doesn't answer what I need: Combining two sorted
I am trying to learn how to get all the img src from a
I'm trying to self-study C using C Primer Plus from Stephen Prata and one
I'm doing a programming question from C++ Primer Plus which asks me to make
What the title says. Example excerpted from the haskellwiki prime numbers page: {-# OPTIONS_GHC
From what I've read about Tasks, the following code should cancel the currently executing

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.