是的,从容器中删除 Foo* 会破坏 Foo*,但不会释放 Foo。销毁原始指针总是是无操作的。不能有别的办法!让我给你几个理由。
存储类
删除指针只有在指针对象实际上是动态分配的情况下才有意义,但是运行时如何知道指针变量被销毁时是否是这种情况?指针也可以指向静态和自动变量,删除其中一个会产生undefined behavior。
{
Foo x;
Foo* p = &x;
Foo* q = new Foo;
// Has *q been allocated dynamically?
// (The answer is YES, but the runtime doesn't know that.)
// Has *p been allocated dynamically?
// (The answer is NO, but the runtime doesn't know that.)
}
悬空指针
无法确定指针是否在过去已被释放。两次删除同一个指针会产生undefined behavior。 (第一次删除后它变成了一个悬空指针。)
{
Foo* p = new Foo;
Foo* q = p;
// Has *q already been released?
// (The answer is NO, but the runtime doesn't know that.)
// (...suppose that pointees WOULD be automatically released...)
// Has *p already been released?
// (The answer WOULD now be YES, but the runtime doesn't know that.)
}
未初始化的指针
根本无法检测指针变量是否已经初始化。猜猜当您尝试删除这样的指针时会发生什么?再一次,答案是undefined behavior。
{
Foo* p;
// Has p been properly initialized?
// (The answer is NO, but the runtime doesn't know that.)
}
动态数组
类型系统不区分指向单个对象的指针 (Foo*) 和指向对象数组的第一个元素的指针 (也是 Foo*)。当指针变量被销毁时,运行时可能无法确定是通过delete 还是通过delete[] 释放指针。通过错误的形式释放调用undefined behavior。
{
Foo* p = new Foo;
Foo* q = new Foo[100];
// What should I do, delete q or delete[] q?
// (The answer is delete[] q, but the runtime doesn't know that.)
// What should I do, delete p or delete[] p?
// (The answer is delete p, but the runtime doesn't know that.)
}
总结
由于运行时无法对指针执行任何合理的操作,因此销毁指针变量总是无操作。什么都不做肯定比由于不知情的猜测而导致未定义的行为要好:-)
建议
考虑使用智能指针作为容器的值类型,而不是原始指针,因为它们负责在不再需要指针时释放指针。根据您的需要,使用 std::shared_ptr<Foo> 或 std::unique_ptr<Foo> 。如果您的编译器还不支持 C++0x,请使用 boost::shared_ptr<Foo>。
Never,我再说一遍,永远不会使用 std::auto_ptr<Foo> 作为容器的值类型。