开发者

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

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

delete vs delete[] operators in 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

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?


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


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.


  • 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.

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


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

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

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


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:

 delete []p;

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


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


Raymond Chen provides a detailed description of how scaler and vector delete works in his blog titled 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


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.

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.


In addition, 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜