Click here to Skip to main content
13,145,068 members (58,607 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 29 Aug 2010

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

, 31 Aug 2010
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.


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 3 not useful after Skott Meyers' "Effe... Pin
halex20057-Oct-10 8:47
memberhalex20057-Oct-10 8:47 
GeneralReason for my vote of 3 Reading "effective c " book solve t... Pin
halex20057-Oct-10 8:38
memberhalex20057-Oct-10 8:38 
GeneralIt would be interesting to add that early in C++ days there ... Pin
Nemanja Trifunovic30-Sep-10 5:30
memberNemanja Trifunovic30-Sep-10 5:30 
GeneralThere is a common myth among programmers that it's a good id... Pin
Aescleal30-Aug-10 0:13
memberAescleal30-Aug-10 0:13 
GeneralSorry. I just modified the tip. Thank you Pin
elitehussar29-Aug-10 20:40
memberelitehussar29-Aug-10 20:40 
GeneralI am sure, what are you trying to do!? inline text mentioned... Pin
ThatsAlok29-Aug-10 20:29
memberThatsAlok29-Aug-10 20:29 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 31 Aug 2010
Article Copyright 2010 by elitehussar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid