Brandon Tiqui
Brandon Tiqui

Reputation: 1439

Dynamic Memory Allocation in C++

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

Upvotes: 0

Views: 681

Answers (3)

Alex Martelli
Alex Martelli

Reputation: 881665

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...!]]

Upvotes: 3

Quintin Robinson
Quintin Robinson

Reputation: 82335

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

Upvotes: 5

Andrew
Andrew

Reputation: 12009

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.

Upvotes: 2

Related Questions