Virus721
Virus721

Reputation: 8315

Questions about custom allocators

I would like to create my own allocator class, and I wonder a few things :

For the function deallocate defined below, can i just ignore the number of items to deallocate ?

void deallocate( pointer p, size_type nNum )
{
   (void) nNum;
   delete[] p;
}

..

template< class U >
struct rebind
{
    typedef Allocator< U > other;
};

template< class U >
Allocator( const Allocator< U > & oAlloc )

Thank you. :)

Upvotes: 3

Views: 92

Answers (1)

Axel Borja
Axel Borja

Reputation: 3974

  1. This is your allocator, so you can use the type you want. But, note, that if you expect to use it with STL containers, you will have to use a type that STL containers expect. size_t seems appropriate here.

  2. You can provide parameters to your allocator like if it was a normal class. Constructors, initialization methods or setters are fine. In fact, you can provide all functionalities you want to your allocator, but you have to respect the allocate, deallocate signature.

  3. You can ignore the size of your deallocate function. I saw a lot of standard allocator implementations that did not use this parameter. This information could be usefull sometimes, for example if your allocator switch to different allocation strategies depending on size, this parameter could be helpfull in deallocate method to switch on the good deallocate implementation.

Upvotes: 2

Related Questions