[ACCEPTED]-C++ STL allocator vs operator new-allocator

Accepted answer
Score: 50

For general programming, yes you should 28 use new and delete.

However, if you are writing a 27 library, you should not! I don't have your 26 textbook, but I imagine it is discussing 25 allocators in the context of writing library 24 code.

Users of a library may want control 23 over exactly what gets allocated from where. If 22 all of the library's allocations went through 21 new and delete, the user would have no way to have 20 that fine-grained level of control.

All STL 19 containers take an optional allocator template 18 argument. The container will then use that 17 allocator for its internal memory needs. By 16 default, if you omit the allocator, it will 15 use std::allocator which uses new and delete (specifically, ::operator new(size_t) and 14 ::operator delete(void*)).

This way, the user of that container can 13 control where memory gets allocated from 12 if they desire.

Example of implementing a 11 custom allocator for use with STL, and explanation: Improving Performance with Custom Pool Allocators for STL

Side Note: The 10 STL approach to allocators is non-optimal 9 in several ways. I recommend reading Towards a Better Allocator Model for 8 a discussion of some of those issues.

Edit in 2019: The 7 situation in C++ has improved since this 6 answer was written. Stateful allocators 5 are supported in C++11, and that support was improved 4 in C++17. Some of the people involved in 3 the "Towards a Better Allocator Model" were 2 involved in those changes (eg: N2387), so that's 1 nice (:

Score: 1

The two are not contradictory. Allocators 29 are a PolicyPattern or StrategyPattern used 28 by the STL libraries' container adapters 27 to allocate chunks of memory for use with 26 objects.

These allocators frequently optimize 25 memory allocation by allowing * ranges 24 of elements to be allocated at once, and 23 then initialized using a placement new * items 22 to be selected from secondary, specialized 21 heaps depending on blocksize

One way or another, the 20 end result will (almost always) be that 19 the objects are allocated with new (placement 18 or default)


Another vivid example would be 17 how e.g. boost library implements smartpointers. Because 16 smartpointers are very small (with little 15 overhead) the allocation overhead might 14 become a burden. It would make sense for 13 the implementation to define a specialized 12 allocator to do the allocations, so one 11 may have efficient std::set<> of smartpointers, std::map<..., smartpointer> etc.

(Now 10 I'm almost sure that boost actually optimizes 9 storage for most smartpointers by avoiding 8 any virtuals, therefore the vft, making 7 the class a POD structure, with only the 6 raw pointer as storage; some of the example 5 will not apply. But then again, extrapolate 4 to other kinds of smartpointer (refcounting 3 smartpointers, pointers to member functions, pointers 2 to member functions with instance reference 1 etc. etc.))

More Related questions