[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppDelete
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].
If you have a function like:
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:
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:
:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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_deletestruct 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; }
- include <boost/shared_ptr.hpp>
Code links
- ::, scope operator
- <<, stream out operator
- #include
- argc
- argv
- assert
- char
- cout
- endl
- for
- #include
- int
- iostream
- main
- return
- scope operator,::
- std
- stream out operator, <<
References
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4
- 2) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6
- 3) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 3: Use const whenever possible.
- 4) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 13: Use objects to manage resources.
- 5) Scott Meyers. Effective C++ (3rd edition).ISBN: 0-321-33487-6. Item 18: Make interface easy to use correctly and hard to use incorrectly.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
