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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:09:00+00:00 2026-05-10T22:09:00+00:00

I know how they are different syntactically, and that C++ uses new, and C

  • 0

I know how they are different syntactically, and that C++ uses new, and C uses malloc. But how do they work, in a high-level explanation?

See What is the difference between new/delete and malloc/free?

  • 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. 2026-05-10T22:09:01+00:00Added an answer on May 10, 2026 at 10:09 pm

    I’m just going to direct you to this answer: What is the difference between new/delete and malloc/free? . Martin provided an excellent overview. Quick overview on how they work (without diving into how you could overload them as member functions):

    new-expression and allocation

    1. The code contains a new-expression supplying the type-id.
    2. The compiler will look into whether the type overloads the operator new with an allocation function.
    3. If it finds an overload of an operator new allocation function, that one is called using the arguments given to new and sizeof(TypeId) as its first argument:

    Sample:

    new (a, b, c) TypeId;  // the function called by the compiler has to have the following signature: operator new(std::size_t size, TypeOfA a, TypeOfB b, TypeOf C c); 
    1. if operator new fails to allocate storage, it can call new_handler, and hope it makes place. If there still is not enough place, new has to throw std::bad_alloc or derived from it. An allocator that has throw() (no-throw guarantee), it shall return a null-pointer in that case.
    2. The C++ runtime environment will create an object of the type given by the type-id in the memory returned by the allocation function.

    There are a few special allocation functions given special names:

    • no-throw new. That takes a nothrow_t as second argument. A new-expression of the form like the following will call an allocation function taking only std::size_t and nothrow_t:

    Example:

    new (std::nothrow) TypeId; 
    • placement new. That takes a void* pointer as first argument, and instead of returning a newly allocated memory address, it returns that argument. It is used to create an object at a given address. Standard containers use that to preallocate space, but only create objects when needed, later.

    Code:

    // the following function is defined implicitly in the standard library void * operator(std::size_t size, void * ptr) throw() {     return ptr; } 

    If the allocation function returns storage, and the the constructor of the object created by the runtime throws, then the operator delete is called automatically. In case a form of new was used that takes additional parameters, like

    new (a, b, c) TypeId; 

    Then the operator delete that takes those parameters is called. That operator delete version is only called if the deletion is done because the constructor of the object did throw. If you call delete yourself, then the compiler will use the normal operator delete function taking only a void* pointer:

    int * a = new int; => void * operator new(std::size_t size) throw(std::bad_alloc); delete a; => void operator delete(void * ptr) throw();  TypeWhosCtorThrows * a = new ('argument') TypeWhosCtorThrows; => void * operator new(std::size_t size, char const* arg1) throw(std::bad_alloc); => void operator delete(void * ptr, char const* arg1) throw();  TypeWhosCtorDoesntThrow * a = new ('argument') TypeWhosCtorDoesntThrow; => void * operator new(std::size_t size, char const* arg1) throw(std::bad_alloc); delete a; => void operator delete(void * ptr) throw(); 

    new-expression and arrays

    If you do

    new (possible_arguments) TypeId[N]; 

    The compiler is using the operator new[] functions instead of plain operator new. The operator can be passed a first argument not exactly sizeof(TypeId)*N: The compiler could add some space to store the number of objects created (necassary to be able to call destructors). The Standard puts it this way:

    • new T[5] results in a call of operator new[](sizeof(T)*5+x), and
    • new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know they're using a jQuery plugin, but I can't seem to find which
Is there any real use for self modifying code ? I know that they
I know that embedding CSS styles directly into the HTML tags they affect defeats
We all know what virtual functions are in C++, but how are they implemented
I know how that question looks, but I'm quite serious. I am trying to
in my code I obtain two different lists from different sources, but I know
I hear people writing these programs all the time and I know what they
I'm just curious about this really, does anyone know why they broke convention on
In Toyota manufacturing lines they always know what path a part have traveled. Just
I know PHP scripts don't actually compile until they are run. However, say I

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.