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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:49:47+00:00 2026-05-25T15:49:47+00:00

As far as the respective language standards go, C offers dynamic memory allocation only

  • 0

As far as the respective language standards go, C offers dynamic memory allocation only through the malloc() family, while in C++ the most common form of allocation is performed by ::operator new(). The C-style malloc is also available in C++, and many “baby’s first allocator” examples use it as its core allocation function, but I am curious how contemporary compilers implement the actual production operator-new.

Is it just a thin wrapper around malloc(), or is it implemented fundamentally differently on account of the rather different memory allocation behaviour of a typical C++ program compared to a typical C program?

[Edit: I believe the main difference is usually described as follows: A C program has fewer, larger, long-lived allocations, while a C++ program has many, small, short-lived allocations. Feel free to chime in if that’s mistaken, but it sounds like one would benefit from taking this into account.]

For a compiler like GCC it would be easy to just have one single core allocation implementation and use that for all relevant languages, so I wonder if there are differences in the details that try to optimize the resulting allocation performance in each language.


Update: Thanks for all the great answers! It looks like in GCC this is completely solved by ptmalloc, and that MSVC also uses malloc at the core. Does anyone know how the MSVC-malloc is implemented?

  • 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-25T15:49:48+00:00Added an answer on May 25, 2026 at 3:49 pm

    Here is the implementation used by g++ 4.6.1:

    _GLIBCXX_WEAK_DEFINITION void *
    operator new (std::size_t sz) throw (std::bad_alloc)
    {
      void *p;
    
      /* malloc (0) is unpredictable; avoid it.  */
      if (sz == 0)
        sz = 1;
      p = (void *) malloc (sz);
      while (p == 0)
        {
          new_handler handler = __new_handler;
          if (! handler)
    #ifdef __EXCEPTIONS
            throw bad_alloc();
    #else
            std::abort();
    #endif
          handler ();
          p = (void *) malloc (sz);
        }
    
      return p;
    }
    

    This is found in libstdc++-v3/libsupc++/new_op.cc inside the g++ source distro.

    As you can see, it’s a fairly thin wrapper around malloc.

    edit On many systems it is possible to fine-tune the behaviour of malloc, typically by calling mallopt or setting environment variables. Here is one article discussing some features available on Linux.

    According to Wikipedia, glibc versions 2.3+ use a modified version of the allocator called ptmalloc, which itself is a derivative of dlmalloc designed by Doug Lea. Interestingly, in an article about dlmalloc Doug Lea gives the following perspective (emphasis mine):

    I wrote the first version of the allocator after writing some C++
    programs that almost exclusively relied on allocating dynamic memory.
    I found that they ran much more slowly and/or with much more total
    memory consumption than I expected them to. This was due to
    characteristics of the memory allocators on the systems I was running
    on (mainly the then-current versions of SunOs and BSD ). To counter
    this, at first I wrote a number of special-purpose allocators in C++,
    normally by overloading operator new for various classes. Some of
    these are described in a paper on C++ allocation techniques that was
    adapted into the 1989 C++ Report article Some storage allocation
    techniques for container classes.

    However, I soon realized that building a special allocator for each
    new class that tended to be dynamically allocated and heavily used was
    not a good strategy when building kinds of general-purpose programming
    support classes I was writing at the time. (From 1986 to 1991, I was
    the the primary author of libg++ , the GNU C++ library.) A broader
    solution was needed — to write an allocator that was good enough
    under normal C++ and C loads
    so that programmers would not be tempted
    to write special-purpose allocators except under very special
    conditions.

    This article presents a description of some of the main design goals,
    algorithms, and implementation considerations for this allocator.

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

Sidebar

Related Questions

I'm relatively new to version control, and so far only have experience working with
The VCL form designer offers pink guidelines for aligning controls at their respective text
Far as best practices are concerned, which is better: public void SomeMethod(string str) {
As far as I know, foreign keys (FK) are used to aid the programmer
So far I have encountered adjacency list, nested sets and nested intervals as models
As far as I know, Flash has to pass info off to another external
As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...)
As far as i know it is not possible to do the following in
As far as variable naming conventions go, should iterators be named i or something
As far as I can tell, this is isn't possible, so I'm really just

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.