Click here to Skip to main content
Click here to Skip to main content

Tagged as

C++ Tip: Aware of the confusion between delete with delete[]

, 31 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
There is a common myth among C++ programmers that it is okay to use delete instead of delete [] to release dynamically allocated arrays (via new) for built-in types. For example,
int *p=new int[10];
delete p; /*bad; should be: delete[] p*/
 
This is totally wrong. The C++ standard specifically says that using delete to release dynamically allocated arrays of any type yields undefined behavior. The fact that, on some platforms, applications that use delete instead of delete [] do not crash; can be attributed to sheer luck.
 
Visual C++, for instance, implements both delete[] and delete for built-in types by calling free() function. However, there is no guarantee that future releases of Visual C++ will adhere to this convention. Furthermore, there is no guarantee that this code will work on other compilers.

To conclude, using delete instead of delete[] and vice versa is hazardous and should be avoided.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

elitehussar

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 3 not useful after Skott Meyers' "Effe... Pinmemberhalex20057-Oct-10 9:47 
GeneralReason for my vote of 3 Reading "effective c " book solve t... Pinmemberhalex20057-Oct-10 9:38 
GeneralIt would be interesting to add that early in C++ days there ... PinmemberNemanja Trifunovic30-Sep-10 6:30 
GeneralThere is a common myth among programmers that it's a good id... PinmemberAescleal30-Aug-10 1:13 
GeneralSorry. I just modified the tip. Thank you Pinmemberelitehussar29-Aug-10 21:40 
GeneralI am sure, what are you trying to do!? inline text mentioned... PinmemberThatsAlok29-Aug-10 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 31 Aug 2010
Article Copyright 2010 by elitehussar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid