Does delete[] deallocate the entire block of memory?
Asked Answered
T

3

6

Consider the following:

char* msg = new char[20]; 
msg[4] = '\0'; 
delete[] msg; 
  1. Did the delete[] msg deallocate all the 20 chars allocated for msg or just those up to the \0?
  2. If it deallocated only up to the \0, how can I force it to delete the entire block of memory?
Tongue answered 3/12, 2012 at 17:25 Comment(1)
Of course not. If so, you couldn't alloc anything else than a string.Embrey
B
10

The original code in your question had undefined behaviour, since you were using delete with new[].

I notice that you have fixed it by replacing the delete with delete[]:

delete[] msg;

This is correct, and will deallocate all memory that's been allocated by new[].

There is no concept of "deleting till \0", or any other such "partial" deletion. Only complete blocks allocated with new/new[] can be deleted, so your code is fine.

Blabber answered 3/12, 2012 at 17:26 Comment(6)
In many implementations (e.g. clang's libstdc++), new & new[] are synonymous with malloc, and delete and delete[] for free. That means that it won't make a difference in those scenarios.Steinbok
OK , I changed , and now ?Tongue
@RichardJ.RossIII: Firstly, new can't be synonymous with malloc since it has different semantics (calls the constructor). Even if the three are based on the same allocator, this doesn't change the fact that technically the behaviour is undefined. I can show you lots of other examples which happen to work on some platforms using some compilers, but that rely on undefined behaviour and are therefore not portable.Blabber
@Richard J. Ross III: Even if new/delete uses malloc/free underneath, the former calls constructors/destructors for you, while the latter does not. That is a significant difference in semantics.Wore
@RichardJ.RossIII That's like saying there's xyz implementation that does garbage collection, so you never need to use delete. How is it relevant at all?Malamud
@RichardJ.RossIII: Even if the particular internal details of how those operations allocate memory match that is still irrelevant: There are huge differences between new and new[] (and delete/delete[]) and they got nothing to do with memory allocation per se. Please don't provide incomplete, inaccurate or blatantly wrong answers or comments. It does a disservice to everyone - yourself included.Refill
H
5

The delete[] operator deletes the entire block of memory. You cannot deallocate part of block of memory – you can only deallocate the entire block.

This is also the case for new/delete, and malloc/free. The deallocating function can only deallocate the entire block of memory.

Hawkshaw answered 3/12, 2012 at 17:28 Comment(0)
L
1

You should use delete[] to free an array. That's the only way you can tell the program to look for array information and be able to free the whole array.

With a normal delete the compiler may think that you want to deallocate just a single pointer and this is not the case. The extra information about the size of the array and whatever won't be looked for and extra memory won't be freed.

The fact that you set a char inside the array to \0 doesn't make any difference, when releasing the array only the known size of the array is considered. Otherwise anything similar to

Class *array = new Class[50];
array[0] = NULL;
delete[] array;

would just leak memory.

Lubberly answered 3/12, 2012 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.