c++memorystlallocator

If I overload the global new operator to use a memory pool to allocate memory, will this overloaded 'new' affect STL memory allocation?


Currently a project in my company overloaded the global new/delete.

I test for some short code, with the global operator new overloaded. And I see the overloaded new is called when I use STL(like vector, etc.) So I want to know will the 'new' suppress the stl allocator and will there be a efficiency dropdown?


Solution

  • All allocator-aware containers use an instance of the allocator_type for dynamic memory management.

    This means that the overload of the global operator new / operator delete does not directly affect STL containers, but affects the std::allocator because it uses the global operator new and operator delete for the implementation of the allocate() and deallocate() member functions, respectively. Since the allocator-aware containers use the std::allocator as default allocator, by transitive property, the overload of the above operators may fall on STL containers.

    To avoid that, it is necessary to implement a custom allocator.