-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Expert C++
By :
The idea behind an allocator is to provide control to container memory management. In simpler words, an allocator is an advanced garbage collector for C++ containers. Although we discuss allocators in the scope of container memory management, you can expand the idea to a generic garbage collector. At the beginning of this section, we implemented a badly designed garbage collector. When examining allocators, you will find a lot of similarities between the poorly designed GarbageCollector class and the default allocator in C++. Defined in <memory>, the default allocator has two basic functions – allocate() and deallocate(). The allocate() function is defined as follows:
[[nodiscard]] constexpr T* allocate(std::size_t num);
The allocate() function acquires space for num objects of the T type. Pay attention to the [[nodiscard]] attribute – it means that the return value should not be discarded by the caller. The compiler will...