Is it guaranteed that the standard C ++ library libraries call replaceable new functions?

If I replace all operator new signatures that I can, at least with the versions that I tested, I see that the standard containers call my replaced versions to allocate memory.

Is this guaranteed by the standard? That is, would it be illegal for an implementation to use an optimized version that would not call my replacement functions for the memory that underlies standard containers?

+21
c ++ language-lawyer c ++ 11
Oct 19 '17 at 5:16
source share
2 answers

The default dispenser for containers that support dispensers, such as std::vector<T> , is std::allocator<T> . This class template is described in the [default.allocator] section of the standard. According to [allocator.members] / 6 in C ++ 14:

storage is obtained by calling ::operator new(std::size_t)

So, the new global operator is the one you need to replace. If you overloaded operator new specifically for T , this overload will not be used by the default allocator.

+27
Oct 19 '17 at 5:24 on
source share

Is this guaranteed by the standard?

Until you use a custom dispenser to instantiate a container, I believe that is true.

From http://en.cppreference.com/w/cpp/memory/allocator

The class std::allocator is the default Allocator used by all standard library containers unless a user-assigned std::allocator is specified.

and

From http://en.cppreference.com/w/cpp/memory/allocator/allocate :

Allocates n * sizeof(T) bytes of uninitialized storage by calling ::operator new(std::size_t)

+7
Oct 19 '17 at 5:22 on
source share



All Articles