Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ delete array memory without brackets still works? [duplicate]

int* arr = new int[count];

delete arr;

Why does this work? I've checked and it actually frees the memory. From what I've read I need delete[] arr; otherwise it won't actually free all the memory.

like image 756
Farzher Avatar asked Aug 04 '13 19:08

Farzher


People also ask

How delete [] is different from delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

What happens if you use Delete instead of Delete []?

You will get undefined behavior. Save this answer.

How does delete [] know how many elements to delete?

The general answer is that the C++ runtime stores the number objects allocated (not the bytes allocated) so that when delete[] is called it can execute the right number of destructors.

What happens when delete an array?

When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.


3 Answers

The difference is not whether or not the allocated memory is properly freed - whether you use

delete

or

delete[]

the memory will still be properly deallocated.

The difference is whether or not the destructors will be properly invoked.

delete // will only invoke the destructor of the first element of the array.

delete[] // will invoke the destructor for *each* element of the array.

This has no practical effect for primitive types like int or float, but when you have an array of some class, the difference can be critical.

like image 113
Zenilogix Avatar answered Sep 22 '22 07:09

Zenilogix


Now try it with filling the array with strings of 100 bytes each, and see if it still frees all the allocated memory...

It is undefined behaviour, and as always, sometimes UB will appear to work. In your case, you have no destructor for the objects in the memory, so there is no "further work", just free all the memory [1]. But if you have an object that has a destructor that does something useful, it (probably) won't get called.

You should ALWAYS use delete [] if you used new T[size]; to allocate. Don't mix the two, it's always wrong - just sometimes it HAPPENS to work [just like SOME sizes of spanners in inches works on mm nuts and vice versa - but it's still wrong to use a inches spanner set on metric nuts].

[1] Note that this may work for this particular compiler/C++ library combination. Compiling it with a different compiler, using a different C++ library, or compiling for a different OS may cause it to crash when you try the same thing.

like image 31
Mats Petersson Avatar answered Sep 26 '22 07:09

Mats Petersson


delete and delete [] are actually different operators and to use the wrong one is always an error. The problem is that it often seems fine at the time, but the heap has ben corrupted and you are very likely to experience an apparently unrelated crash later.

like image 24
David Elliman Avatar answered Sep 24 '22 07:09

David Elliman