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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:36:46+00:00 2026-05-24T10:36:46+00:00

Many C++ programmers have suffered from the fierce clashes with the global C++ objects

  • 0

Many C++ programmers have suffered from the fierce clashes with the global C++ objects initialization/cleanup. Eventually I’ve found a good enough solution to this problem, I’ve been using (and anjoying) it for years now. My question is: does this solution fully comply to the C++ standard, or it’s “platform/implementation-dependent”?

The problem

Generally-speaking there are two major problems with global objects:

  • Unpredictable order of their construction/destruction. This bites if those objects depend on each other.
  • The construction/destruction code is executed during the CRT initialization/cleanup outside the main program entry point. There’s no way to wrap this code with try/catch, or perform any preliminary initialization.

One way to overcome those issues is not using global objects at all. Instead one may use static/global pointers to those objects. During the program initialization those objects are either allocated dynamically or instantiated as automatic variables within the entry point function (main), and their pointers stored in those pointers. By such you have the full control over your “global” objects lifetime.

However this method also has some drawbacks. It’s related to the fact that not only the creation/destruction of those objects is different, but also their access is different. Normally a global object resides in the data section, which is allocated by the loader, and its virtual address is known at the build time. Using global pointers leads to the following drawbacks:

  • Somewhat slower object access, extra pointer dereferencing. During the runtime instead of assuming the object is at the specified address the compiler generates the code that dereferences the global pointer.
  • Weaker optimizations. The compiler may not realize that the pointer always points to the same object.
  • If the actual objects are allocated on heap:
    • Worse performance (heap allocations are “heavy”)
    • Memory fragmentation
    • Chance of out-of-memory exception
  • If the actual objects are allocated on stack (auto variables in main):
    • Stack size is usually limited. In some circumstances its consumption by “fat” objects is suboptimal.

The solution

The solution I’ve found is to override the object’s new/delete operatiors.

// class definition
class MyObject
{
    // some members
    // ...

    static char s_pMyPlaceholder[];

public:

    // methods
    // ...

    static MyObject& Instance()
    {
        return *(MyObject*) s_pMyPlaceholder;
    }

    void* operator new (size_t) { return s_pMyPlaceholder; }
    void operator delete (void*) {}
};

// object placeholder instantiated
char MyObject::s_pMyPlaceholder[sizeof(MyObject)];

void main()
{
    // global initialization
    std::auto_ptr<MyObject> pMyObj(new MyObject);

    // run the program
    // ...

}

The trick is to allocate enough space in the global memory (by declaring a global array of the adequate size), and then use fictive memory allocation for the needed object, that will “allocate” this global memory”. By such we achieve the following:

  • Semantically we allocate the object dynamically. Hence we have the full control over its lifetime.
  • Actually the object resides in the global memory. Hence all the drawbacks related to the “pointer-wise” method are inapplicable to our case.
  • The object is visible everywhere in the program. One calls MyObject::Instance() to get the reference to it. And, BTW, this function call is easily inlined by the compiler.

So that everything seems ok with this method. I’m just curious if it’s legal from the C++ standard perspective.

  • 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-24T10:36:47+00:00Added an answer on May 24, 2026 at 10:36 am

    I don’t think you have a formal guarantee that your solution works on every compliant implementation, because the C++ standard doesn’t guarantee that statically allocated arrays of char are aligned as would be required by any object of the same size.

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

Sidebar

Related Questions

Recently, many programmers and that includes me, have taken the X out of AJAX,
Not really a programming question, but relevant to many programmers... Let's say I have
There are many skills a programmer could have (understanding the problem, asking good questions,
Many beginning programmers write code like this: sub copy_file ($$) { my $from =
I have an issue that I feel many programmers can relate to... I have
I have a number of COBOL programmers who are moving to .NET. I've found
I have read that when hashing a password, many programmers recommend using the BCrypt
Let's say I have to read from a directory that has many large XML
I don't have enough experience with Qt yet to make a good design choice.
Many C/C++/Fortran and other programmers would have come across stack overflow errors. My question

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.