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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:25:05+00:00 2026-06-13T05:25:05+00:00

1) I have some static classes in my project that allocate variables within their

  • 0

1) I have some static classes in my project that allocate variables within their constructors.

class StaticClass
{
public:
    char *var;
    StaticClass()
    {
        var=new char[100];
    }
};
static StaticClass staticClass;

2) I have overridden the new and delete operators and made them keep track of all current allocations in a std::unordered_map

unordered_map<void*,size_t> allocations;

void* operator new[](size_t size)
{
    void *p=malloc(size); 
    if (p==0) // did malloc succeed?
        throw std::bad_alloc(); // ANSI/ISO compliant behavior
    allocations[p]=size;
    return p;
}

When my program starts, staticClass’s constructor is called before allocations’ constructor is, so operator new() tries to insert size into allocations before it has been initialized, which errors.

Previously, when I ran into problems with the order of static construction, I simply made the std::map into a NULL pointer, and then initialized it the first time it was used, ensuring it would be valid the first time I inserted it:

unsorted_map<void*,size_t> *allocations=NULL;

//in code called by static constructor:
if(allocations==NULL)
    allocations=new unsortedmap()
//now safe to insert into allocations

However, this will no longer work since I would be calling new within operator new(), creating an infinite recursive loop.

I am aware that I could probably solve this by making another special version of operator new that takes some token argument to differentiate it, and just use that to initialize allocations, however in a more general (learning) sense, I would prefer to somehow either

a) force allocations to initialize before StaticClass does (best)

b) have some way to call the default operator new instead of my overridden one (which I don’t think is possible, but…)

c) some other more general solution?

  • 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-13T05:25:06+00:00Added an answer on June 13, 2026 at 5:25 am

    A simple way to avoid initialization order issues is to wrap your static object inside a function:

    unordered_map<void*,size_t> &allocations()
    {
      static unordered_map<void*,size_t> static_map;
      return static_map;
    }
    

    Then use it like this:

    void* operator new[](size_t size)
    {
        void *p=malloc(size); 
        if (p==0) // did malloc succeed?
            throw std::bad_alloc(); // ANSI/ISO compliant behavior
        allocations()[p]=size;
        return p;
    }
    

    However, you still run the risk of std::unordered_map using your operator new internally.

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

Sidebar

Related Questions

I just got a project that have some core classes that uses static methods.
I have some static text that needs to show up at 2 locations within
I have a base and derived class. The base class constructors have some static
I have a public static property in a class. The class has some custom
I have a ASP.NET MVC project that has a static class in it to
I have a base class with a static factory method that makes derived classes:
I have refactored some UIView sub-classes into a static library. However, when using Interface
I have some static resources (images and HTML files) that will be localized. One
I have a windows forms application, where I have declared some static variables. On
I have some async method public static Task<JObject> GetUser(NameValueCollection parameters) { return CallMethodApi(users.get, parameters,

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.