34

C++ has several functions to acquire dynamic storage, most of which differ in some fundamental way. Several more are usually added by the OS.

Two of these are of special interest due to their portability and similarity: malloc and ::operator new.

Are there any differences (w.r.t. the standard and implementation) between the global void* operator new(size_t, ::std::nothrow&) and void* malloc(size_t)?

Since there seems to be some confusion what I am talking about, consider the following two calls:

void* p = ::std::malloc(10);
void* q = ::operator new(10, ::std::nothrow);

The obvious and trivial difference is how to deallocate the memory:

::std::free(p);
::operator delete(q);

Note: This question is not a duplicate of e.g. What is the difference between new/delete and malloc/free? since it talks about using the global operator new that does not actually perform any ctor calls.

plasmacel
  • 8,183
  • 7
  • 53
  • 101
danielschemmel
  • 10,885
  • 1
  • 36
  • 58

3 Answers3

30

The main differences, aside from syntax and free vs. delete, are

  1. you can portably replace ::operator new;
  2. malloc comes with realloc, for which new has no equivalent;
  3. new has the concept of a new_handler, for which there is no malloc equivalent.

(Replacing malloc opens up a can of worms. It can be done, but not portably, because it requires knowledge of the linker.)

Evg
  • 25,259
  • 5
  • 41
  • 83
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • @gha.st If nothing else, then the fact that it's `::std::malloc` and you're not allowed to add your own names to `::std` unless explicitly permitted to do so. – Angew is no longer proud of SO Apr 24 '14 at 14:26
  • Huh, replacing `::operator new` like that is fairly funky – danielschemmel Apr 24 '14 at 14:30
  • 1
    Some research shows that C++03 already contains the concept of a new_handler (§18.4.2), it has only been extended for C++11 – danielschemmel Apr 24 '14 at 14:33
  • @gha.st: you're right, C++11 only introduced `get_new_handler`. Fixed. – Fred Foo Apr 24 '14 at 14:35
  • 1
    (4) `malloc` reports failure as a nullpointer result, `operator new` reports failure via exception, (5) `malloc` is called directly by user code, `operator new` is ordinarily called indirectly via a transaction-like `new` expression that couples allocation and initialization. – Cheers and hth. - Alf Apr 29 '14 at 23:06
  • 1
    @Cheersandhth.-Alf The question is about the non-throwing `new`. – Fred Foo Apr 30 '14 at 08:56
  • The default alignments of the two are also different. `::operator new` will align the allocated memory to `__STDCPP_DEFAULT_NEW_ALIGNMENT__`, while `std::malloc` will align to `alignof(std::max_align_t)`. – plasmacel Jul 22 '20 at 18:57
6

There are two differences I can think of:

  1. Which function you must use to deallocate the memory, operator delete vs. free().

  2. A C++ program can legally provide its own version of ::operator new and that version is guaranteed to be called by new expressions. It's not possible to override malloc with your own version.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

The macroscopic difference I can see without further research is that the throwing variant of the global ::new operator throws std::bad_alloc if the allocation cannot be done, whereas malloc returns NULL. But I do believe that most of the differences listed here apply to the global ::new operator, even if the topic is about new.

Community
  • 1
  • 1
gd1
  • 11,300
  • 7
  • 49
  • 88