new/delete

This is a discussion on new/delete within the C++ Programming forums, part of the General Programming Boards category; Hello, I had a question about what the difference is between delete and delete[]. Lets say for example I have ...

>Will they both free up the memory allocated to tmp?
No, delete [] will free all of the memory allocated to tmp, but delete will only free the memory for the first element. So yes, one would most certainly cause a memory leak.

>Is this valid then?
No, and technically neither is delete on an array allocated by new[]. What happens when you use the wrong delete is dependent on the implementation of the operators. Both are wrong, but the usual reaction to using delete on an array is that the first element is freed but the remaining memory is not. Of course, you can't rely on this.

>but the usual reaction to using delete on an array is that the first element is freed but the remaining memory is not.<

Actually, it doesn't normally have any affect on the immediate de-allocation. Most delete implementations will end up calling free() or something similar which don't care if one or many elements have been allocated; they'll just free a block (as you're no doubt aware).

The problem is that in most implementations using delete on something new[]'d will result in only one destructor being called (instead of many) which could result in a secondary memory leak if you need to de-allocate in your destructor or other resource problems. Because primitives don't have destructors you *may* be able to get away with it; but as you point out, there's nothing in the standard that allows you to rely on this, so it's probably not a good idea.

>I thought the lack of this was why Windows usually starts lagging if you let it run for a long time...
Windows does reclaim the memory, but not right away. So if you run a memory leak intensive program the computer will run slowly for several minutes.

Originally posted by Prelude >I thought the lack of this was why Windows usually starts lagging if you let it run for a long time...
Windows does reclaim the memory, but not right away. So if you run a memory leak intensive program the computer will run slowly for several minutes.

-Prelude

That's right....it does reclaim but not immeadiately....

do something like;

for(;;)new double[1000];

and catch the bad_alloc exception......your PC will be pretty slow for a few minutes.....on XP, there's an emergency utility that launches and tries to set things safe...but that's not instant.....

>Anyway if it does work then why not always use the "[]" syntax for delete?
Because it's wrong not to match the correct new with the correct delete. It may work for you, but that's not guaranteed, especially if you port to another compiler or system.

Anyway if it does work then why not always use the "[]" syntax for delete?

Because its undefined and not for what it was made for.....it may work in a small example, but it wont be trustworthy...if it was I doubt the Standards Board would have let the delete[] operator in

Remember that delete[] expects to call multiple destructors..... you might get away with it on a built in data type, but when you are doing it with class objects, you will probably fall foul at some point...so best bet - use delete[] for new[] and delete for new