Click here to Skip to main content
15,914,074 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralDoc/View inside Dialog based project Pin
-alok-18-May-04 23:21
suss-alok-18-May-04 23:21 
GeneralRe: Doc/View inside Dialog based project Pin
jmkhael18-May-04 23:56
jmkhael18-May-04 23:56 
GeneralRe: Doc/View inside Dialog based project Pin
-alok-19-May-04 23:55
suss-alok-19-May-04 23:55 
GeneralSetSysColors permanent in profile Pin
thilol18-May-04 23:20
thilol18-May-04 23:20 
Generaldelete vs delete [] Pin
Johann Gerell18-May-04 23:07
Johann Gerell18-May-04 23:07 
GeneralRe: delete vs delete [] Pin
jmkhael18-May-04 23:38
jmkhael18-May-04 23:38 
GeneralRe: delete vs delete [] Pin
RChin19-May-04 1:56
RChin19-May-04 1:56 
GeneralRe: delete vs delete [] Pin
Diddy19-May-04 2:12
Diddy19-May-04 2:12 
RChin wrote:
Having a destructor has nothing to do with the functionality of the delete []

I think what he is saying is why bother with delete [] when char doesn't have a d'tor - ie it doesn't matter that scalar delete doesn't call all the d'tors as a char doesn't have one.

In general

What happens if you allocate with scalar "new" and free with vector "delete[]"?
The scalar "new" will allocate a single object with no hidden counter. The vector "delete[]" will look for the hidden counter, which isn't there, so it will either crash (accessing nonexistent memory) or grab a random number and attempt to destruct that many items. If the random number is greater than one, you will start corrupting memory after the object. If the random number is zero, you fail to destruct anything. If the random number is exactly one, then the one object is destructed.

Next, the vector "delete[]" will attempt to free the memory block starting one size_t in front of the actual memory block. Depending on how the heap feels today, this may be detected as an invalid parameter and ignored, or this can result in heap corruption.

What happens if you allocate with vector "new[]" and free with scalar "delete"?
The vector "new[]" allocates several objects and stores the "howmany" in the hidden counter. The scalar "delete" destructs the first object in the vector. If it was a vector of zero objects, you corrupted memory. If it was a vector of two or more objects, then objects 2 an onward will not be destructed. (Result: Memory or other leak.)

Next, the scalar "delete" will free the memory block directly, which will fail because the memory block actually starts at the hidden size_t in front of the vector. This again corrupts the heap since you are freeing memory that is not a valid heap pointer.

For more info:

http://weblogs.asp.net/oldnewthing/archive/2004/02/03/66660.aspx

RChin wrote:
The general rule I tend to live by when allocating memory using new and delete is to use its complement

Errrr yeah well they wouldn't bother having both scalar and vector deletes and new's if that wasn't the case eh.

The general question was why thats the case. Many people seem under the misconception that the difference between delete and delete[] is just that delete[] will call all d'tors, not just the one for the first object - and hence it's safe to use delete on a new[] if your object doesn't have a d'tor. That web link explains what else happens when you mix/use scalar and vector new/delete.
GeneralRe: delete vs delete [] Pin
Johann Gerell19-May-04 2:53
Johann Gerell19-May-04 2:53 
GeneralRe: delete vs delete [] Pin
RChin19-May-04 3:17
RChin19-May-04 3:17 
GeneralRe: delete vs delete [] Pin
Diddy19-May-04 5:49
Diddy19-May-04 5:49 
GeneralRe: delete vs delete [] Pin
Dennis Gourjii19-May-04 2:08
Dennis Gourjii19-May-04 2:08 
GeneralRe: delete vs delete [] Pin
David Crow19-May-04 3:40
David Crow19-May-04 3:40 
GeneralRe: delete vs delete [] Pin
Johann Gerell19-May-04 4:09
Johann Gerell19-May-04 4:09 
GeneralRe: delete vs delete [] Pin
Michael Dunn19-May-04 3:52
sitebuilderMichael Dunn19-May-04 3:52 
GeneralRe: delete vs delete [] Pin
Tim Smith19-May-04 3:59
Tim Smith19-May-04 3:59 
GeneralRe: delete vs delete [] Pin
bikram singh19-May-04 6:03
bikram singh19-May-04 6:03 
GeneralRe: delete vs delete [] Pin
Diddy25-May-04 23:55
Diddy25-May-04 23:55 
GeneralOLE Pin
Aizaz18-May-04 22:35
Aizaz18-May-04 22:35 
GeneralRe: OLE Pin
Maxwell Chen18-May-04 22:41
Maxwell Chen18-May-04 22:41 
GeneralRe: OLE Pin
Diddy19-May-04 2:00
Diddy19-May-04 2:00 
GeneralRe: OLE Pin
Member 42102519-May-04 6:17
Member 42102519-May-04 6:17 
GeneralCFile and .bmp file... network Pin
Maverick18-May-04 22:14
Maverick18-May-04 22:14 
GeneralRe: CFile and .bmp file... network Pin
David Crow19-May-04 3:56
David Crow19-May-04 3:56 
GeneralBluetooth Pin
Superman718-May-04 21:27
Superman718-May-04 21:27 

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.