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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:23:59+00:00 2026-05-29T17:23:59+00:00

A Schwartz counter is intended to ensure that a global object is initialized before

  • 0

A Schwartz counter is intended to ensure that a global object is initialized before it is used.

Please consider the use of a Schwartz counter shown below.

File Foo.h:

class Foo
{
   Foo::Foo();
};

File Foo.cpp:

#include "Foo.h"

// Assume including Mystream.h provides access to myStream and that
// it causes creation of a file-static object that initializes
// myStream (aka a Schwartz counter).
#include "MyStream.h"

Foo::Foo()
{
   myStream << "Hello world\n";
}

If Foo::Foo() runs after main() starts, the use of myStream is guaranteed to be safe (i.e. myStream will have been initialized before use) because of the file-static initializer object mentioned in the comments.

However, suppose the Foo instance gets created before main() starts, as would happen if it were global. This is shown here:

File Global.cpp:

#include "Foo.h"

Foo foo;

Note that Global.cpp does not get a file-static initializer object like Foo.cpp does. In this case, how does the Schwartz counter ensure that the MyStream initializer (and therefore the MyStream object itself) are initialized before foo? Or can the Schwartz counter fail in this case?

  • 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-29T17:24:16+00:00Added an answer on May 29, 2026 at 5:24 pm

    The use of “Schwartz counters” (so called after Jerry Schwartz who designed the basics of the IOStreams library as it is now in the standard; note that he can’t be blamed for many of odd choices because these were tagged onto the original design) can result in accesses to objects before they are constructed. The most obvious scenario is calling a function during construction of a global object which calls into another translation unit using the its own global constructed via a Schwartz counter (I’m using std::cout as the global being guarded by a Schwartz counter to keep the example short):

    // file a.h
    void a();
    
    // file a.cpp
    #include <iostream>
    void a() { std::cout << "a()\n"; }
    
    // file b.cpp
    #include <a.h>
    struct b { b() { a(); } } bobject;
    

    If the global objects in file b.cpp are constructed prior to those in the file a.cpp and if std::cout is constructed via a Schwartz counter with a.cpp being the first instance, this code will fail. There is are at least two other reasons why Schwartz counters don’t work particularly well:

    1. When using a global object for this, this objects ends up being constructed twice. Although this works in practice when done right, I think this is ugly. A work-around for this is to use a char buffer of appropriate size for the actual definition of the object (these typically get mangled to the name as an object of the correct type). However, in both of these cases things are messy.
    2. When the global objects guarded by a Schwartz counter are used in many translation units (as it the case for std::cout) this may cause a significant start-up delay: well-written code typically doesn’t use any global initialization but the Schwartz counter needs to run a piece of code for each of the object files which needs to be loaded.

    Personally I have come to the conclusion that this technique is a nifty idea but it doesn’t work in practice. There are three approaches I use instead:

    1. Don’t use global objects. This make this whole discussion obsolete and works best especially in concurrent code. Where a global resource is absolutely needed, a function static object returned by reference and initialized using std::call_once() is a much better alternative.
    2. Placing the global object at an appropriate location when linking the executable (e.g. last) causes it to be initialized first. I had experimented with this in the past and back then I found that I can place object files appropriately on all systems I cared for. The main drawback here is that there are no guarantees and things might change when switching between compiler versions. For the C++ standard library this is, however, acceptable (and I only cared about the global stream objects when I did this).
    3. Put the global objects into a dedicated shared library: when the shared library is loaded, its initialization code is executed. The objects in the shared library become available only after the initialization is complete. I found that this works reliably but requires an extra library.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Randal Schwartz says that he uses Git's SVN import/export feature when he has to
I have written a small test program in which I try to use the
When I use static variables in C++, I often end up wanting to initialize
In his Thinking in C++ (Chapter 10) Eckel describes a technique that was pioneered
I would like to have an UIImageView that flickers. I thougt I can make
How can i use numbers in my yml files to show translations? f.e.: #
I'm working on a new version of an already released code of perl, and
I've implemented a task using the async Sockets pattern in Silverlight 3. I started
I am using a named system mutex to synchronise 2 processes. This is how
So I have an interesting design problem. I am working on SLES 9+ Linux,

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.