c++ [duplicate]中删除[]与删除[]的区别

时间:2022-01-15 13:21:58

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

可能的重复:删除vs删除[]操作符在c++。

I've written a class that contains two pointers, one is char* color_ and one in vertexesset* vertex_ where vertexesset is 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?

现在它工作得很好。这两者之间有什么区别?

9 个解决方案

#1


54  

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

当你创建数组类型时,你删除[],当你没有创建数组类型时,你删除[]。例子:

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 [].

#2


12  

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

删除和删除[]不是一回事!*解释了这一点。简而言之,delete[]在分配的数组中的每个元素上调用析构函数,而delete假设您只有一个实例。您应该使用新的foo[]分配数组,并使用delete[]删除它们;对于普通对象,使用new和delete。在非数组上使用delete[]可能会导致大破坏。

#3


7  

  • If you allocate with malloc(), you use free()
  • 如果使用malloc()进行分配,则使用free()
  • If you allocate with new you use delete
  • 如果使用new分配,则使用delete
  • If you allocate with new[] you use delete[]
  • 如果你使用new[]进行分配,你会使用delete[]
  • If you construct with placement-new you call the destructor direct
  • 如果你用place -new构造,你可以直接调用析构函数
  • 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.
  • 如果使用智能指针是有意义的,那么就使用它们,不要费事调用delete(但仍然需要调用new)。匹配的删除将在智能指针中。

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

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

#4


5  

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

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

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

如果使用的是操作符new,则必须使用操作符delete,不使用方括号。

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

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

#5


4  

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

当我们想释放分配给对象指针的内存时,就使用“delete”。

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.

注意:即使删除p,也可以释放内存,但只能释放第一个元素内存。

#6


1  

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

如果您有有效的c++第1部分,请参考第5条:在new和delete的相应用法中使用相同的表单。

#7


0  

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 delete on 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 new or new[] are called so that the correct form of deletion is used regardless, but again that is implementation dependent and not guaranteed.

当调用新的或新的[]时,关于如何分配对象的信息是可能的,这样无论如何都可以使用正确的删除形式,但这是依赖于实现的,并且不能保证。

#8


0  

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在他的博客《不匹配标量和向量新与删除》中详细描述了标量和向量删除的工作方式。

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?

#9


0  

in addition (obviously my typing speed should be improved :), consider not using pointers if you don't really have to. e.g. char* can be replaced with std::string, and if your vertexesset member is not polymorphic, you can make it a member object. In this case, you wouldn't need delete at all

此外(显然我的打字速度应该得到改进:),如果你不需要的话,可以考虑不使用指针。例如,可以用std::string替换char*,如果vertexesset成员不是多态性的,可以将其作为成员对象。在这种情况下,您根本不需要删除

#1


54  

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

当你创建数组类型时,你删除[],当你没有创建数组类型时,你删除[]。例子:

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 [].

#2


12  

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

删除和删除[]不是一回事!*解释了这一点。简而言之,delete[]在分配的数组中的每个元素上调用析构函数,而delete假设您只有一个实例。您应该使用新的foo[]分配数组,并使用delete[]删除它们;对于普通对象,使用new和delete。在非数组上使用delete[]可能会导致大破坏。

#3


7  

  • If you allocate with malloc(), you use free()
  • 如果使用malloc()进行分配,则使用free()
  • If you allocate with new you use delete
  • 如果使用new分配,则使用delete
  • If you allocate with new[] you use delete[]
  • 如果你使用new[]进行分配,你会使用delete[]
  • If you construct with placement-new you call the destructor direct
  • 如果你用place -new构造,你可以直接调用析构函数
  • 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.
  • 如果使用智能指针是有意义的,那么就使用它们,不要费事调用delete(但仍然需要调用new)。匹配的删除将在智能指针中。

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

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

#4


5  

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

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

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

如果使用的是操作符new,则必须使用操作符delete,不使用方括号。

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

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

#5


4  

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

当我们想释放分配给对象指针的内存时,就使用“delete”。

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.

注意:即使删除p,也可以释放内存,但只能释放第一个元素内存。

#6


1  

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

如果您有有效的c++第1部分,请参考第5条:在new和delete的相应用法中使用相同的表单。

#7


0  

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 delete on 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 new or new[] are called so that the correct form of deletion is used regardless, but again that is implementation dependent and not guaranteed.

当调用新的或新的[]时,关于如何分配对象的信息是可能的,这样无论如何都可以使用正确的删除形式,但这是依赖于实现的,并且不能保证。

#8


0  

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在他的博客《不匹配标量和向量新与删除》中详细描述了标量和向量删除的工作方式。

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?

#9


0  

in addition (obviously my typing speed should be improved :), consider not using pointers if you don't really have to. e.g. char* can be replaced with std::string, and if your vertexesset member is not polymorphic, you can make it a member object. In this case, you wouldn't need delete at all

此外(显然我的打字速度应该得到改进:),如果你不需要的话,可以考虑不使用指针。例如,可以用std::string替换char*,如果vertexesset成员不是多态性的,可以将其作为成员对象。在这种情况下,您根本不需要删除