C++中delete和delete[]的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4670782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:06:36  来源:igfitidea点击:

The difference between delete and delete[] in C++

c++memory-managementdelete-operator

提问by SIMEL

Possible Duplicate:
delete vs delete[] operators in C++

可能的重复:
C++ 中的 delete 与 delete[] 运算符

I've written a class that contains two pointers, one is char* color_and one in vertexesset* vertex_where vertexessetis a class I created. In the destractor I've written at start

我已经写了包含两个指针类,一个是char* color_和一个在vertexesset* vertex_那里vertexesset是一类我创建。在我开始时写的析构函数中

delete [] color_;
delete [] vertex_;

When It came to the destructor it gave me a segmentation fault.

当它涉及到析构函数时,它给了我一个分段错误。

Then I changed the destructor to:

然后我将析构函数更改为:

delete [] color_;
delete vertex_;

And now it works fine. What is the difference between the two?

现在它工作正常。两者有什么区别?

回答by etarion

You delete []when you newed an array type, and deletewhen you didn't. Examples:

delete []当你new编一个数组类型,delete当你没有。例子:

typedef int int_array[10];

int* a = new int;
int* b = new int[10];
int* c = new int_array;

delete a;
delete[] b;
delete[] c; // this is a must! even if the new-line didn't use [].

回答by EmeryBerger

deleteand delete[]are not the same thing! Wikipedia explainsthis, if briefly. In short, delete []invokes the destructor on every elementin the allocated array, while deleteassumes you have exactly one instance. You should allocate arrays with new foo[]and delete them with delete[]; for ordinary objects, use newand delete. Using delete[]on a non-array could lead to havoc.

delete而且delete[]不是一回事!维基百科解释了这一点,如果简短的话。简而言之,对分配的数组中的每个元素delete []调用析构函数,同时假设您只有一个实例。您应该使用;分配数组并删除它们;对于普通对象,使用和。使用非阵列上可能会导致混乱。deletenew foo[]delete[]newdeletedelete[]

回答by CashCow

  • If you allocate with malloc(), you use free()
  • If you allocate with new you use delete
  • If you allocate with new[] you use delete[]
  • If you construct with placement-new you call the destructor direct
  • If it makes sense to use vector rather than new[] then use it
  • If it makes sense to use smart-pointers then use them and don't bother to call delete (but you'll still need to call new). The matching delete will be in the smart-pointer.
  • 如果使用 malloc() 分配,则使用 free()
  • 如果您使用 new 分配,则使用 delete
  • 如果您使用 new[] 分配,则使用 delete[]
  • 如果你用placement-new构造你直接调用析构函数
  • 如果使用 vector 而不是 new[] 有意义,那么使用它
  • 如果使用智能指针有意义,那么使用它们并且不要打扰调用 delete(但你仍然需要调用 new)。匹配的删除将在智能指针中。

https://isocpp.org/wiki/faq/freestore-mgmt

https://isocpp.org/wiki/faq/freestore-mgmt

回答by Francesco

You have to use delete []if you allocated memory on the heap with operator new[](e.g. a dynamic array).

delete []如果您使用运算符new[](例如动态数组)在堆上分配内存,则必须使用。

If you used operator new, you must use operator delete, without the square brackets.

如果使用 operator new,则必须使用 operator delete,不带方括号。

It is not related to deleting a built-in type or a custom class.

它与删除内置类型或自定义类无关。

回答by Developer

When we want to free a memory allocated to a pointer to an object then "delete" is used.

当我们想要释放分配给指向对象的指针的内存时,使用“删除”。

int * p;
p=new int;

// now to free the memory 
delete p;

But when we have allocated memory for array of objects like

但是当我们为对象数组分配内存时

int * p= new int[10]; //pointer to an array of 10 integer

then to free memory equal to 10 integers:

然后释放等于 10 个整数的内存:

 delete []p;

NOTE: One can free the memory even by delete p;, but it will free only the first element memory.

注意:即使通过 也可以释放内存delete p;,但它只会释放第一个元素的内存。

回答by yasouser

If you have Effective C++ part 1refer to Item #5: Use the same form in corresponding uses of new and delete.

如果您有Effective C++ 第 1 部分,请参阅第 5 项:在 new 和 delete 的相应用法中使用相同的形式。

回答by Tergiver

Raymond Chen provides a detailed description of how scaler and vector delete works in his blog titled Mismatching scalar and vector new and delete.

Raymond Chen 在其名为Mismatching scalar and vector new and delete 的博客中详细描述了缩放器和向量删除的工作原理。

Here's a link to the InformIT article that is mis-linked in the above article: http://www.informit.com/articles/article.aspx?p=30642

这是上面文章中错误链接的 InformIT 文章的链接:http: //www.informit.com/articles/article.aspx?p=30642

回答by davka

In addition, consider not usingpointers if you don't reallyhave to. e.g. char*can be replaced with std::string, and if your vertexessetmember is not polymorphic, you can make it a member object. In this case, you wouldn't need deleteat all

另外,可以考虑不使用指针,如果你不真的有。egchar*可以替换为std::string,如果您的vertexesset成员不是多态的,您可以将其设为成员对象。在这种情况下,你就不需要delete在所有

回答by Clifford

And now it works fine.

现在它工作正常。

More by luck that judgement if it does, and are you certain that it is really working?

更幸运的是,判断是否确实如此,您确定它真的有效吗?

The destructor for every object must be called, the delete[]operator uses information set by new[]to determine how many objects to destroy. So while deleteon its own may recover the memory (though whether it does or not is implementation dependent), it may not call the destructor for each object allocated.

必须调用每个对象的析构函数,delete[]操作符使用设置的信息new[]来确定要销毁多少个对象。因此,虽然delete它自己可以恢复内存(尽管它是否依赖于实现),但它可能不会为每个分配的对象调用析构函数。

It is possible for the information about how the object's were allocated to be included when newor new[]are called so that the correct form of deletion is used regardless, but again that is implementation dependent and not guaranteed.

有可能在调用newnew[]调用时包含关于对象如何分配的信息,以便使用正确的删除形式,但这同样取决于实现并且不能保证。