gpt4 book ai didi

c++ - std::list::remove 方法是否调用每个已删除元素的析构函数?

转载 作者:IT老高 更新时间:2023-10-28 12:52:26 25 4
gpt4 key购买 nike

我有代码:

std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);

std::list::remove 方法是否调用每个已删除元素的析构函数(和释放内存)?如果是这样,我该如何避免?

最佳答案

是的,删除 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>作为容器的值类型。

关于c++ - std::list::remove 方法是否调用每个已删除元素的析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4260464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com