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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:10:50+00:00 2026-05-20T11:10:50+00:00

When you have a static global variable in a C++ header file, each translation

  • 0

When you have a static global variable in a C++ header file, each translation unit that includes the header file ends up with its own copy of the variable.

However, if I declare a class in that same header file, and create a member function of that class, implemented inline within the class declaration, that uses the static global variable, for example:

#include <iostream>

static int n = 10;

class Foo {
 public:
   void print() { std::cout << n << std::endl; }
};

then I see slightly odd behavior under gcc 4.4:

  1. If I compile without optimization, all uses of the member function use the copy of the variable from one of the translation units (the first one mentioned on the g++ command line).

  2. If I compile with -O2, each use of the member function uses the copy of the variable from the translation unit in which the case is made.

Obviously this is really bad design, so this question is just out of curiosity. But my question, nonetheless, is what does the C++ standard say about this case? Is g++ behaving correctly by giving different behavior with and without optimization enabled?

  • 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-20T11:10:51+00:00Added an answer on May 20, 2026 at 11:10 am

    The standard says (3.2/5):

    There can be more than one definition
    of a class type (clause 9),
    … provided the definitions satisfy
    the following requirements … in each
    definition of D, corresponding names,
    looked up according to 3.4, shall
    refer to an entity defined within the
    definition of D, or shall refer to the
    same entity

    This is where your code loses. The uses of n in the different definitions of Foo do not refer to the same object. Game over, undefined behavior, so yes gcc is entitled to do different things at different optimization levels.

    3.2/5 continues:

    except that a name can refer to a
    const object with internal or no
    linkage if the object has the same
    integral or enumeration type in all
    definitions of D, and the object is
    initialized with a constant expression
    (5.19), and the value (but not the
    address) of the object is used, and
    the object has the same value in all
    definitions of D

    So in your example code you could make n into a static const int and all would be lovely. It’s not a coincidence that this clause describes conditions under which it makes no difference whether the different TUs “refer to” the same object or different objects – all they use is a compile-time constant value, and they all use the same one.

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

Sidebar

Related Questions

I have used a static global variable and a static volatile variable in file
You can have different naming convention for class members, static objects, global objects, and
I have several service classes that have static methods and offer a service to
I have a static class that I would like to raise an event as
I have some static images in a folder on my IIS 6-based website that
I got a Utility module since VB.NET doesn't have static class like C# and
I have a static library written in C++ and I have a structure describing
I have several static factory patterns in my PHP library. However, memory footprint is
I have a static library *.lib created using MSVC on windows. The size of
I have a static linkable library of C and Fortran routines compiled and linked

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.