views:

244

answers:

3

What is the difference between the 'delete' and 'dispose' C++ operators with regards to dynamic memory allocation?

+2  A: 

I'm assuming this is related to .NET managed C++ because standard C++ has no sense of "dispose".

Delete will release the memory used to store the object; this memory returns to the heap and can be used for other storage requirements.

Dispose will give the object the chance to release resources it acquired such as file handles, etc. Standard C++ would see this sort of task done in the destructor.

Andrew
+5  A: 

delete will free memory dynamically allocated in unmanaged C++
Dispose will force custom object implemented maintenance of disposable objects in managed C++/CLI

Quintin Robinson
+3  A: 

There's no dispose operator (or standard function, or anything of that ilk) in standard C++ -- are you thinking of some specific proprietary framework, such as Microsoft's .NET "managed C++"? delete is standard, it's indeed an operator, and must behave like this in any standard conforming implementation -- that's the main difference with operator dispose, and also the main difference with operators unicorn, toothfairy, and santaclaus, which have a lot in common since none of those exists!-) [[Sorry Virginia...!]]

Alex Martelli
lol... top answer, just to add Dispose is a hack of a hack and only forced onto the air-contaminated Redmond designers back in 2000/1 when they finally listened to the community and introduced a pattern and later a keyword.But it was too late and few years following they started bloating everything Java fmrk style,most glaring example being in service and UI tech.Good that they realised 'unmanaged' resources are all that actually exists in the physical, computing and other galaxies across the universe. Alas, some things never change,despite a moment or two of humble listening at c#@msdn.com..
rama-jka toti