Boost.Container combat: writing an extended bwd test allocator program

Boost.Container combat: writing an extended bwd test allocator program

Boost.Container is an open source, high-quality C++ container library that provides a wealth of container types and algorithms, which can bring us great convenience for writing efficient and reliable C++ programs. When using Boost.Container, we usually need to provide it with an appropriate memory allocator to meet specific needs. This article will demonstrate how to implement an extended bwd test allocator program using Boost.Container.

First, we need to define an allocator class BwdAllocator inherited from bwd_allocator, and implement the allocate and deallocate methods in it. Here we simply print out the allocated/released memory block size for subsequent debugging.

#include <boost/container/allocator.hpp>
#include <iostream>

template <typename T>
class BwdAllocator : public boost::container::bwd_allocator<T>
{
public:
    using Base = boost::container::bwd_allocator<T>;

    template <typename U>
    struct rebind
    {
        typedef BwdAllocator<U> other;
    };

    pointer allocate(size_type n, const_void_pointer hint = 0)
    {
        std::cout << "Allocating " << n * sizeof(T) << " bytes" << std::endl;
        return

Guess you like

Origin blog.csdn.net/Jack_user/article/details/132436520