45

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?

jww
  • 97,681
  • 90
  • 411
  • 885
SIMEL
  • 8,745
  • 28
  • 84
  • 130

9 Answers9

69

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 [].
etarion
  • 16,935
  • 4
  • 43
  • 66
  • 9
    +1 for mentioning the case with int_array. – Bart van Ingen Schenau Jan 12 '11 at 16:06
  • 2
    Copying from the comment to the other answer: It is a bad idea to use array typedefs, but it's not a bad idea to mention it, people get bitten by it. Also, the point is that even when you _wrote_ `new`, you sometimes need to _write_ `delete[]`. – etarion Jan 12 '11 at 16:48
  • Someone edited the typedef - the way it is now (and has been originally) is the correct one, please try your "fixes" before you edit. See http://ideone.com/fYh6MK vs http://ideone.com/w9fPKr – etarion Mar 18 '13 at 19:49
  • I'm interested to know what you would require if you had the following: char** strings = new char*[2]; strings[0]=new char[10]; strings[1] = new char[10]; Would delete [] strings clear all the memory or just the string array, leaving me to clear the two char arrays? – Mahen Oct 07 '13 at 07:34
  • @Mahen it would just delete the pointers to the strings, delete the array contents beforehand. – etarion Oct 07 '13 at 19:22
25

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.

EmeryBerger
  • 3,897
  • 18
  • 29
  • 3
    This should be the answer, it actually explains the difference. Thanks. – Andrew May 13 '15 at 23:51
  • 1
    yea this lost 40 upvotes because it was 2 minutes later. important to note you can't use delete[] as a catch-all solution to handle raw pointers. – jiggunjer Jul 02 '15 at 18:45
  • Why doesn't the C++ runtime just figure out the difference between array and non-array types so that programmers can just use a single `delete` statement? – Carl G Feb 03 '20 at 23:47
  • It would have to be the compiler, not the runtime. The runtime only sees calls to `malloc` and `free` (for example). – EmeryBerger Feb 08 '20 at 02:04
9
  • 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

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
CashCow
  • 30,981
  • 5
  • 61
  • 92
  • `If you allocate with new you use delete` - this is not always the case, see my answer. – etarion Jan 12 '11 at 16:01
  • @etarion: I see nothing in your answer that contradicts this statement; your use of a type alias sill invokes `new[]` not `new`, the type alias simply obfuscates that fact and is probably a bad idea (even to mention it!). – Clifford Jan 12 '11 at 16:22
  • 1
    It is a bad idea to use array typedefs, but it's not a bad idea to mention it, people get bitten by it. Also, the point is that even when you _wrote_ `new`, you sometimes need to _write_ `delete[]`. – etarion Jan 12 '11 at 16:47
5

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.

NAND
  • 663
  • 8
  • 22
Francesco
  • 3,200
  • 1
  • 34
  • 46
2

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.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
Developer
  • 8,390
  • 41
  • 129
  • 238
  • `new int(10)` allocates a single `int`, not an array of 10 `int`. – CB Bailey Jan 12 '11 at 16:05
  • ***NOTE: One can free the memory even by delete p;, but it will free only the first element memory.*** This note is wrong. The following question explains why: [https://stackoverflow.com/questions/4480722/should-new-new-match-delete-delete](https://stackoverflow.com/questions/4480722/should-new-new-match-delete-delete) – drescherjm Jan 02 '20 at 19:48
1

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

yasouser
  • 5,113
  • 2
  • 27
  • 41
1

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

Tergiver
  • 14,171
  • 3
  • 41
  • 68
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.

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

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

NAND
  • 663
  • 8
  • 22
davka
  • 13,974
  • 11
  • 61
  • 86