像这样在作用域外使用delete是不正确的吗

Is it incorrect to use delete out of scope like this?

本文关键字:不正确 delete 作用域 像这样      更新时间:2023-10-16
int* func()
{
   int* i=new int[3];
   return i; 
}
void funcc()
{
   int* tmp=func();
   //delete allocated memory after use
   delete[] tmp;
}

我的观点是,编译器在编译funcc时无法知道有3 int要删除,例如,如果func在另一个文件中。

这在C++FAQ lite:中有介绍

[16.14]在p=new Fred[n]之后,编译器如何知道有n删除[]p期间要销毁的对象

简言之:魔术。

长答案:运行时系统存储对象的数量n,如果你只知道指针p,它就可以被检索到。有两种流行的技术可以做到这一点。这两种技术由商业级编译器使用,两者都有折衷,并且两者都不完美。这些技术是:

  • 过度分配数组,并将n放在第一个Fred对象的左侧
  • 使用以p为键、n为值的关联数组

这就是delete的使用方式,很好。数组大小存储在幕后的某个位置,因此delete[]无论在哪里发生都会做正确的事情。

然而,像这样处理指针很容易出错:在删除数组之前很容易丢失指针,或者逻辑变得如此复杂,以至于你最终删除了同一个数组两次。你最好使用一个类来为你管理阵列:

#include <vector>
std::vector<int> func() {
    return std::vector<int>(3);
}
void funcc() {
    auto tmp = func();
    // no need to do anything - the vector frees its memory automatically
}