'delete' operator

'delete' and 'delete []'

Page 1 of 1

4 Replies - 1419 Views - Last Post: 08 January 2010 - 08:12 AM Rate Topic: -----

#1 pingking123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 19-November 09

'delete' operator

Posted 08 January 2010 - 04:09 AM

I've noticed that if you mismatch 'delete' and 'delete []', an exception is NOT thrown:

int *Array, *Single;
Array = new int [100];
Single = new int;

delete [] Single; // Wrong, but an exception is NOT thrown.
delete Array; // Wrong, but an exception is NOT thrown.



Does anyone knows what is the effect of using 'delete' and 'delete []' in the above (wrong) way? I'm asking because I'm dealing with dynamic memory allocation often, and I want to know what to expect if I do the above mistake.

Is This A Good Question/Topic? 0
  • +

Replies To: 'delete' operator

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: 'delete' operator

Posted 08 January 2010 - 05:44 AM

Expect memory leaks, and if your program does this often enough it may run out of available memory and crash.

The argument given to delete is supposed to be a pointer previously used as the argument to the new operator. Otherwise, the delete operator's behavior is undefined.

Similarly, the argument given to delete[] is supposed to be a pointer previously used as the argument to the new[] operator. Otherwise, the delete[] operator's behavior is undefined.

Since the behavior is undefined, exactly what these operators do when given invalid arguments may vary depending on the compiler. So just don't do that.
Was This Post Helpful? 1
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 'delete' operator

Posted 08 January 2010 - 07:24 AM

Quote

So just don't do that.

best advice... The difference between the two operators is that one deletes a single instance of an object, and the other deletes an array of them. For ints they both probably do the same thing, but for objects (where deconstructors have to be run) things are different. The delete operator will not necessarily scroll though the array and properly destroy all of the objects (it will probably just call the deconstructor for the first one, and then release all of the memory).

Technically though swapping these operators is "undefined" meaning tht the compiler might release the memory correctly on one platform, and not on another platform -- or do it right without optimizations, and then do it incorrectly when optimizing.
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,443
  • Joined: 25-December 09

Re: 'delete' operator

Posted 08 January 2010 - 07:51 AM

Also this is a good reason to use vector and string instead of int[] and char[] whenever possible.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 cdk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 17-January 07

Re: 'delete' operator

Posted 08 January 2010 - 08:12 AM

Here's an example program that 'proves' that the delete operator deletes less than a delete [] operator. It's one of the sample interview questions I wrote for an article about C++ job interview questions.

The example program was tailored for a Borland compiler but if you remove the #include <vcl.h>, then you should be able to compile it on a non-Borland compiler.

The #include <vcl.h> was needed because the sample program was compiled on a Borland compiler and the program threw an access violation at runtime (because of the illegal use of delete instead of delete[]).

http://www.decompile...ers_Page_11.htm

The program uses a global variable to track the number of instantiations of an object.

Have fun with the program and modify it to use the right kind of delete to see how the output changes.

cdk
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1