Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL container library - Is it legal to call allocate/deallocate on different instances of the allocator class?

First of all, I don't think it is. But, I've observed such a behavior with MSVC 10.0 in Debug mode. I'm using a custom allocator class which relies on the user to pass only pointers allocated on the same instance to deallocate. However, in Release mode, my code is working.

Is this a bug or am I mistaken?

like image 837
0xbadf00d Avatar asked Dec 13 '25 00:12

0xbadf00d


1 Answers

The standard requires that any allocator be able to deallocate memory produced by any other allocator of the same type, even if it's a totally different instance. This is required to get list::splice working correctly. It's largely considered a design flaw in the C++ spec, and in C++0x they're introducing a set of fixups to allocators to rememdy this. In the meantime, any allocator you use in the STL containers must not have its own local state.

EDIT: For those of you who want the original language on this, here's §20.1.5/4 of the C++ ISO spec:

Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table 32.

— All instances of a given allocator type are required to be interchangeable and always compare equal to each other.

In the latest ISO draft of the C++0x standard, this requirement is no longer present. The default std::allocator will still maintain this invariant as required, but it doesn't look like you'll have to constrain yourself this way in the future.

like image 130
templatetypedef Avatar answered Dec 14 '25 13:12

templatetypedef