STL 容器库 - 在分配器类的不同实例上调用分配/解除分配是否合法?
首先,我认为不是。但是,我在调试模式下的 MSVC 10.0 中观察到了这种行为。我使用的是自定义allocator
类,它依赖于用户仅将在同一实例上分配的指针传递给deallocate
。但是,在发布模式下,我的代码可以正常工作。
这是一个错误还是我弄错了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该标准要求任何分配器都能够释放由相同类型的任何其他分配器生成的内存,即使它是完全不同的实例。这是让
list::splice
正常工作所必需的。这在很大程度上被认为是 C++ 规范中的一个设计缺陷,在 C++0x 中,他们向分配器引入了一组修复程序来弥补这一点。同时,您在 STL 容器中使用的任何分配器都不能有自己的本地状态。编辑:对于那些想要原始语言的人,这里是 C++ ISO 规范的 §20.1.5/4:
在最新的 C++0x 标准 ISO 草案中,此要求已不再存在。默认的 std::allocator 仍然会根据需要保持这个不变量,但看起来您将来不必以这种方式约束自己。
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:
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.