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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:38:21+00:00 2026-05-18T06:38:21+00:00

Suppose I need to overload global ::operator new() for storing extra data with each

  • 0

Suppose I need to overload global ::operator new() for storing extra data with each allocated object. So basically it would work this way:

  • for each call to global ::operator new() it will take the object size passed and add the size of extra data
  • it will allocate a memory block of size deduced at previous step
  • it will offset the pointer to the part of the block not occupied with extra data and return that offset value to the caller

::operator delete() will do the same in reverse – shift the pointer, access extra data, deallocate memory.

Now the question is how do I allocate memory? Of course I can call malloc() or some platform-specific function (that’s how it is usually done). But normally when I need to allocate raw memory in C++ I call ::operator new(). Can I call the original ::operator new() to do the memory allocation from inside my overloaded global ::operator new()?

  • 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-18T06:38:22+00:00Added an answer on May 18, 2026 at 6:38 am

    You can’t access them because it isn’t really overloading, it’s replacement. When you define your own ::operator new, the old one goes away. That’s pretty much that.

    Essentially, you need to call malloc from a custom ::operator new. Not only that, but also follow the directions in 18.4.1.1/4 to properly handle errors:

    Default behavior:

    — Executes a loop:
    Within the loop, the function first
    attempts to allocate the requested
    storage. Whether the attempt involves
    a call to the Standard C library
    function malloc is unspecified.

    —
    Returns a pointer to the allocated
    storage if the attempt is successful.
    Otherwise, if the last argument to
    set_new_handler() was a null pointer,
    throw bad_alloc.

    — Otherwise, the
    function calls the current new_handler
    (18.4.2.2). If the called function
    returns, the loop repeats.

    — The loop
    terminates when an attempt to allocate
    the requested storage is successful or
    when a called new_handler function
    does not return.

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

Sidebar

Related Questions

Suppose you need to run a program on the world’s fastest supercomputer which will
Suppose you have two seperate ASP.NET Web Application projects that both need to use
Suppose you have a collection of a few hundred in-memory objects and you need
I need to construct some rather simple SQL, I suppose, but as it's a
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose you need to check some condition at multiple places in code. For ex.
I need to write a java script. This is supposed to validate if the
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose we have a table A: itemid mark 1 5 2 3 and table

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.