Learning about the basic allocator

Before we dive into the details of a stateful, unequal allocator, let's review the most basic allocator, which is a stateless, equal allocator. This most basic allocator takes the following form:

template<typename T>class myallocator{public: using value_type = T; using pointer = T *; using size_type = std::size_t;public: myallocator() = default; template <typename U> myallocator(const myallocator<U> &other) noexcept { (void) other; } pointer allocate(size_type n) { if (auto ptr = static_cast<pointer>(malloc(sizeof(T) * n))) { return ptr; } throw std::bad_alloc(); } void deallocate(pointer p, size_type n) { (void) n; return free(p); }};template <typename T1, typename T2>bool operator==(const myallocator<T1> ...

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.