[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppDelete

(C++) delete

Keyword for freeing memory by deleting the space occupied by a dynamically allocated instance. Or: the opposite of new.

delete is not supported in C. The C-counterpart of new and delete are malloc and free.

Prefer the use of auto_ptr's over the use of plain pointers [1,2].

class MyClass
{
  public:
  MyClass()  { std::cout << "Constructor" << std::endl;  
  ~MyClass() { std::cout << "Destructor"  << std::endl;
};
int main()
{
  MyClass * pMyClass = new MyClass;
  //Do stuff
  delete pMyClass;
}


delete and const-correctness using a plain pointer

It is wise to make class interfaces easy to use correctly and hard to use incorrectly [5]. const is important to reach this goal and should be used whenever possible[3]. Below is an example is which only using const alone doesn't suffice.

If you have a function like:

void someFunction(const MyClass * pClass)


you might think that the MyClass pointed by pClass cannot be changed, as pClass is const. This is incorrect, as the following code WILL compile:

void someFunction(const MyClass * pClass)
{
  delete pClass; //OUCH! Deletion of pClass !!!
}


A way to prevent this, is to make MyClass's destructor private. Below is an example in which only main can delete the pointer to MyClass. This is done by making main a friend of MyClass:

struct MyClass
{
  private:
  friend int main(int,char*[]);
  ~MyClass() {}
};
void someFunction(const MyClass * pClass)
{
  //delete pClass; //Yippee! Uncommenting this line will give a compile-error!
}
int main()
{
  MyClass * pClass = new MyClass;
  delete pClass;
  return 0;
}


delete and const-correctness using a smart pointer

It is adviced to use objects to manage resources [4]. So below the example above is repeated using a boost::shared_ptr. Note that it is NOT possible (to the best of my knowledge) to let delete be called by the boost::shared_ptr only. As above, it is still not possible to directly delete a const pointer. But it is possible using boost::checked_delete :
  1. include <boost/shared_ptr.hpp>
struct MyClass { private: friend boost::checked_delete(MyClass*); ~MyClass() {} }; void someFunction(const boost::shared_ptr<MyClass>& pClass) { //delete pClass.get(); //Yippee! Will not compile! //boost::checked_delete(pClass.get()); //OUCH! Will compile! } void someFunction(const MyClass * pClass) { //delete pClass; //Yippee! Will not compile! } int main() { const boost::shared_ptr<MyClass> pClass(new MyClass); return 0; }


Code links

References



last edited (November 11, 2006) by bilderbikkel, Number of views: 4284, Current Rev: 12 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.