Thursday, July 9, 2009

C++ difference between delete and destroy?

In C++, when using the destructor, we use delete to destroy an object. But what's the different between using for example:





delete p;





or





destroy(p);

C++ difference between delete and destroy?
Delete (when used with a pointer) destroys what the pointer points to (e.g., pointer is still there, and can point to something else). Destroy destroys the pointer/object itself.





For instance:





char** charArrayPtr = new char[50];


delete charArrayPtr;


charArrayPtr = NULL;


charArrayPtr = new char[25];





But if you did





destroy(charArrayPtr);





you couldn't use charArrayPtr anymore.





Really, no real need to call destroy explicitly, for the most part.
Reply:There is NO DESTROY FUNCTION IN STANDARD C++. nogoodaddress's answer is wrong and the code is broken (you use delete[], NOT delete, to free arrays). "You couldn't use charArrayPtr anymore" is simply untrue.





You can define a destroy function, which then does whatever you make it do. Report It

Reply:Dyanamic memory allocated should need to be freed so that it becomes available for future request of dynamic memory.It is the work of delete keyword.





ex:





delete pointer; //for single element


or


delete [] pointer; //for multiple element





And


Destroy keyword in c++ is used for destroying the specified objects.
Reply:delete, removes it completely.


destroy, stops it


No comments:

Post a Comment