about "delete"
-
delete[] new -> delete new[] -> delete[]
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
I'd like to stress PJ's point that you should *always* use delete [] with new []. Otherwise strange things will happen. Trust me. :) -- Schni Schna Schnappi! Schnappi Schnappi Schnapp!
-
I'd like to stress PJ's point that you should *always* use delete [] with new []. Otherwise strange things will happen. Trust me. :) -- Schni Schna Schnappi! Schnappi Schnappi Schnapp!
I'd suggest he (almost) never uses new[] and delete[] and make use of container classes instead. Here is why[^]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
I'd suggest he (almost) never uses new[] and delete[] and make use of container classes instead. Here is why[^]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
I allways used
delete
regardless if usednew
ornew[]
...it worked fine (and I see no reason why it should not...)byte buffer[4] = new byte[4]; // 4 bytes DWORD dw4Bytes = new DWORD[1]; // 4 bytes ... delete buffer; // releases 4 bytes delete dw4Bytes; // releases 4 bytes
-
I allways used
delete
regardless if usednew
ornew[]
...it worked fine (and I see no reason why it should not...)byte buffer[4] = new byte[4]; // 4 bytes DWORD dw4Bytes = new DWORD[1]; // 4 bytes ... delete buffer; // releases 4 bytes delete dw4Bytes; // releases 4 bytes
rubicon_hfr wrote: ...it worked fine (and I see no reason why it should not...) Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
rubicon_hfr wrote: ...it worked fine (and I see no reason why it should not...) Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak. Since the topic has the subject "about delete" I only mention that there is no difference between delete and delete[]. You may assume that "..." stands for the following:
try { // do something meaningful } catch (...) { }
Of course you can usefinally
to free memory... :rose: Or you just use a wrapper class which ctor does this for you (so you can't forget)... Or use managed C++ :laugh: