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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:15:49+00:00 2026-05-31T18:15:49+00:00

Today I was writing a little Memory Profiler for my code and for the

  • 0

Today I was writing a little Memory Profiler for my code and for the first time overloaded operator new. In doing so, I figured the syntax is void* operator new(size_t sz). This is indeed very similar to malloc where you have void* malloc(size_t sz).

This got me into thinking since when you use malloc, you need to explicitly cast the pointer back to your data type whereas for new you don’t need to do this. How does the compiler figure out the correct data type for new and why do you have to make it return void *? Isn’t T* operator new(size_t sz) more intuitive?

  • 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-31T18:15:51+00:00Added an answer on May 31, 2026 at 6:15 pm

    C++ separates memory allocation and object construction. The allocation function (i.e. operator new()) returns a void * to some memory, and the new operator constructs an object in that memory. If you will, the new operator “converts” memory into an object, and a placement-new expression is a bit like a “cast”:

    void * addr = ::operator new(sizeof(Foo));    // memory
    
    Foo * p = ::new (addr) Foo(1, true, 'a');     // object (note: no cast!)
    
    p->~Foo();                                    // it's almost over
    
    ::operator delete(addr);                      // now it's over!
    

    The default, non-placement form of new performs allocation and construction in one go. It’s morally equivalent to the first half of this example. But still, allocation and construction remain two distinct concepts.

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

Sidebar

Related Questions

I am very new (started today) to writing chrome extensions but need to write
Basically I found myself today writing lots of code like below: #define VAL_1 0
Today I was writing some C code to sort an array of structs using
Today while I was writing some code for two methods that shows and hides
Today is my first day writing Excel macro and I was given a task
I am currently writing a little bootstrap code for a service that can be
In writing some code today, I have happened upon a circumstance that has caused
Im new to android. I have been writing my first app, I testing it
Today while writing some Visual C++ code I have come across something which has
Today i was writing some heavy reflection-using code, and i came across this behavior

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.