0

Reading this question: How to know which malloc is used?, there seem to be a lot of different implementation of malloc.

I was trying to find something similar for the C++ new operator but was not able to find any information about how/where new is implemented and whether different implementations exist. Do they? What does glibc for example use?

Community
  • 1
  • 1
1v0
  • 342
  • 1
  • 9
  • 7
    Does it matter? What some implementation uses today may change tomorrow - or even later today. – Nik Bougalis May 04 '16 at 20:20
  • 2
    You can [write your own allocator for your C++ objects](http://stackoverflow.com/questions/826569/compelling-examples-of-custom-c-allocators), that's the primary difference. Internally C++ has its own method for allocating that differs from `malloc()` and related libraries. – tadman May 04 '16 at 20:22
  • new is overridable, which gives you more convenience. – Ethan F. May 04 '16 at 20:24
  • 3
    Unless you have a very special need you shouldn't need to care. And if you do, you can write your own allocator. – Jesper Juhl May 04 '16 at 20:24
  • 2
    `new` is the C++ language construct (keyword), not C library or C++ library. It's up to compiler vendor to implement it. So you need to lookup your favorite compiler's source code for that (and if it's Microsoft, then no luck). If you are interested in memory allocation, you might also want to take a look at my never_will_be_answered [thread](http://stackoverflow.com/questions/16377991/how-to-control-memory-allocation-strategy-in-third-party-library-code) about memory allocation hooking. – Ivan Aksamentov - Drop May 04 '16 at 20:25
  • 1
    @Drop Thanks for your comment. @ Nik Bougalis Yep it matters (sometimes), if you do parallel programming and allocate objects on the free space, there has to be an efficient! mechanism to keep track of the (un)allocated space, I was curious to know how this mechanism works on the system where I run my code – 1v0 May 04 '16 at 20:33
  • 1
    @Drop - `new` is, indeed, a keyword. However, `operator new` is part of the C++ library, and is responsible for allocating the memory that the keyword `new` calls for. – Pete Becker May 04 '16 at 20:34
  • @tadman Are you implying that (default) new uses malloc underneath? Is that true? – 1v0 May 04 '16 at 20:59
  • @1v0 The opposite actually. I'd be extremely surprised if any modern C++ implementation used anything from libc. – tadman May 04 '16 at 21:05

2 Answers2

1

Here are a couple of allocators I found:

HOARD (GNU + commercial licenses)

MicroQuill SmartHeap for SMP (commercial license)

Google Perf Tools TCMalloc (BSD license)

NedMalloc (BSD license)

JemAlloc (BSD license)

PTMalloc (GNU, no Windows port yet?)

Intel Thread Building Blocks (GNU, commercial)

But I am still curious how to determine the default allocator used by my compiler...

1v0
  • 342
  • 1
  • 9
-1

In my experience the only way to know is to force it:

void* operator new (std::size_t size) throw (std::bad_alloc)
{
    void *ptr = malloc(size);
    if (!ptr) throw std::bad_alloc();
}

void operator delete(void *ptr)
{
    free(ptr);
}

And do the same for the other 3 versions.

Somebody will surely claim you can't replace the standard-library version. If you are dynamically linking with the standard library they are right, so don't do that. If you are statically linking, this works.

Joshua
  • 40,822
  • 8
  • 72
  • 132