C++ 中的 delete 与 delete[] 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2425728/
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
delete vs delete[] operators in C++
提问by shreyasva
What is the difference between delete
and delete[]
operators in C++?
C++ 中的delete
和delete[]
运算符有什么区别?
回答by Nick Meyer
The delete
operator deallocates memory and calls the destructor for a single object created with new
.
该delete
运营商释放内存,并呼吁与创建一个对象析构函数new
。
The delete []
operator deallocates memory and calls destructors for an array of objects created with new []
.
该delete []
运算符释放内存并为使用new []
.创建的对象数组调用析构函数。
Using delete
on a pointer returned by new []
or delete []
on a pointer returned by new
results in undefined behavior.
使用delete
上的指针返回通过new []
或delete []
通过返回的指针new
导致不确定的行为。
回答by Johannes Schaub - litb
The delete[]
operator is used to delete arrays. The delete
operator is used to delete non-array objects. It calls operator delete[]
and operator delete
function respectively to delete the memory that the array or non-array object occupied after (eventually) calling the destructors for the array's elements or the non-array object.
该delete[]
运算符用于删除数组。该delete
运算符用于删除非数组对象。它分别调用operator delete[]
和operator delete
函数在(最终)调用数组元素或非数组对象的析构函数后删除数组或非数组对象占用的内存。
The following shows the relations:
下图显示了关系:
typedef int array_type[1];
// create and destroy a int[1]
array_type *a = new array_type;
delete [] a;
// create and destroy an int
int *b = new int;
delete b;
// create and destroy an int[1]
int *c = new int[1];
delete[] c;
// create and destroy an int[1][2]
int (*d)[2] = new int[1][2];
delete [] d;
For the new
that creates an array (so, either the new type[]
or new
applied to an array type construct), the Standard looks for an operator new[]
in the array's element type class or in the global scope, and passes the amount of memory requested. It may request more than N * sizeof(ElementType)
if it wants (for instance to store the number of elements, so it later when deleting knows how many destructor calls to done). If the class declares an operator new[]
that additional to the amount of memory accepts another size_t
, that second parameter will receive the number of elements allocated - it may use this for any purpose it wants (debugging, etc...).
为new
创建一个阵列(这样,无论是new type[]
或new
应用于阵列型构建体),所述标准长相对于operator new[]
在阵列的元件类型的类或在全球范围内,并传递的存储器请求的量。它可能会请求比N * sizeof(ElementType)
它想要的更多(例如存储元素的数量,以便稍后在删除时知道有多少析构函数调用完成)。如果该类声明operator new[]
除了内存量之外的一个接受另一个size_t
,则第二个参数将接收分配的元素数量 - 它可以将其用于它想要的任何目的(调试等)。
For the new
that creates a non-array object, it will look for an operator new
in the element's class or in the global scope. It passes the amount of memory requested (exactly sizeof(T)
always).
对于new
创建非数组对象的 ,它将operator new
在元素的类或全局范围内查找。它传递请求的内存量(完全sizeof(T)
总是)。
For the delete[]
, it looks into the arrays' element class type and calls their destructors. The operator delete[]
function used is the one in the element type's class, or if there is none then in the global scope.
对于delete[]
,它会查看数组的元素类类型并调用它们的析构函数。使用的operator delete[]
函数是元素类型类中的函数,或者如果没有则在全局范围内。
For the delete
, if the pointer passed is a base class of the actual object's type, the base class must have a virtual destructor (otherwise, behavior is undefined). If it is not a base class, then the destructor of that class is called, and an operator delete
in that class or the global operator delete
is used. If a base class was passed, then the actual object type's destructor is called, and the operator delete
found in that class is used, or if there is none, a global operator delete
is called. If the operator delete
in the class has a second parameter of type size_t
, it will receive the number of elements to deallocate.
对于delete
,如果传递的指针是实际对象类型的基类,则基类必须具有虚拟析构函数(否则,行为未定义)。如果它不是基类,则调用该类的析构函数,并使用operator delete
该类中的或全局operator delete
的。如果传递了基类,则调用实际对象类型的析构函数,并使用operator delete
在该类中找到的对象,如果没有,operator delete
则调用全局对象。如果operator delete
类中有第二个 type 参数size_t
,它将接收要释放的元素数。
回答by ravs2627
This the basic usage of allocate/DE-allocate pattern in c++
malloc
/free
, new
/delete
, new[]
/delete[]
这是 C++ malloc
/ free
、new
/ delete
、new[]
/中分配/DE-分配模式的基本用法
delete[]
We need to use them correspondingly. But I would like to add this particular understanding for the difference between delete
and delete[]
我们需要相应地使用它们。不过,我想补充这个特殊的理解之间的差额delete
,并delete[]
1) delete
is used to de-allocate memory allocated for single object
1)delete
用于释放为单个对象分配的内存
2) delete[]
is used to de-allocate memory allocated for array of objects
2)delete[]
用于释放为对象数组分配的内存
class ABC{}
ABC *ptr = new ABC[100]
when we say new ABC[100]
, compiler can get the information about how many objects that needs to be allocated(here it is 100) and will call the constructor for each of the objects created
当我们说时new ABC[100]
,编译器可以获得有关需要分配多少个对象的信息(这里是 100 个),并将为每个创建的对象调用构造函数
but correspondingly if we simply use delete ptr
for this case, compiler will not know how many objects that ptr
is pointing to and will end up calling of destructor and deleting memory for only 1 object(leaving the invocation of destructors and deallocation of remaining 99 objects). Hence there will be a memory leak.
但相应地,如果我们简单地delete ptr
用于这种情况,编译器将不知道ptr
指向多少个对象,最终只会调用析构函数并删除 1 个对象的内存(留下析构函数的调用和剩余 99 个对象的释放)。因此会有内存泄漏。
so we need to use delete [] ptr
in this case.
所以我们需要delete [] ptr
在这种情况下使用。
回答by san983
The operators delete
and delete []
are used respectively to destroy the objects created with new
and new[]
, returning to the allocated memory left available to the compiler's memory manager.
运算符delete
anddelete []
分别用于销毁用new
and创建的对象new[]
,返回分配给编译器内存管理器可用的内存。
Objects created with new
must necessarily be destroyed with delete
, and that the arrays created with new[]
should be deleted with delete[]
.
用new
来创建的对象必须用 来销毁delete
,用new[]
来创建的数组应该用 来删除delete[]
。
回答by Ekalavya
When I asked this question, my real question was, "is there a difference between the two? Doesn't the runtime have to keep information about the array size, and so will it not be able to tell which one we mean?" This question does not appear in "related questions", so just to help out those like me, here is the answer to that: "why do we even need the delete[] operator?"
当我问这个问题时,我真正的问题是,“两者之间有区别吗?运行时不是必须保留有关数组大小的信息,所以它无法分辨我们指的是哪一个?” 这个问题没有出现在“相关问题”中,所以为了帮助像我这样的人,这里是答案:“为什么我们甚至需要 delete[] 运算符?”
回答by nishant arora
回答by HelloWorld
Well.. maybe I think it is just for explicit.
The operator delete
and operator delete[]
is distinguished but delete
operator can release array too.
嗯..也许我认为这只是为了明确。运算符delete
和运算符delete[]
是有区别的,但delete
运算符也可以释放数组。
test* a = new test[100];
delete a;
And already checked this code have no memeory leak.
并且已经检查过这段代码没有内存泄漏。