Why need a size_type arg in deallocate function?

In alloctor<T> Class as a member function, there is a deallocate().

void deallocate(_Ty* const _Ptr, const size_t _Count)

I thought it works like deallocating element of _Count
from memory block _Ptr points.

So If there are blocks allocated 5 elements and ptr points this block's first.
and when I call deallocate(ptr, 2), just deleted ptr, ptr+1.
but actually, whatever I give a number as _Count, It deallocates whole space allocated.

Why need a second argument?
The system's memory manager (the default allocator) tracks the size of the allocated block on its own, and appears to ignore that parameter.

However, you are required to pass the (entire) size of the block you're deallocating. The internals of some allocators might need that information.
Last edited on
> but actually, whatever I give a number as _Count, It deallocates whole space allocated.

The argument n must be equal to the first argument of the call to allocate() that originally produced p; otherwise, the behavior is undefined.
https://en.cppreference.com/w/cpp/memory/allocator/deallocate


> Why?

The allocator is not required to store the size of the allocated block; the idea is to minimise the overhead (for example, in a pool allocator implementation).
Since users of the allocator would always know the size passed to allocate(), it is easy for them to provide the correct information when calling deallocate().
Thank you for answers, guys! you helped me.
Topic archived. No new replies allowed.