lundi 25 avril 2016

std::allocator deallocate part of memory

I am reading about c++ allocators and the deallocate function has sentence that got my attention:

The argument n must be equal to the first argument of the call to allocate() that originally produced p; otherwise, the behavior is undefined.

Why is that? Why couldn't one deallocate part of the allocated memory, stupid example:

#include <memory>
#include <string>


int main()
{

    std::allocator<std::string> alloc;

    auto const p = alloc.allocate(20);

    alloc.deallocate(p+10, 10);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire