An Example of Smart Pointers

advertisement
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThingBY: GLENN>Foo();
WHEELER
AND MOHAMED
SHAIKH
// calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
template <class T> class Wrapper { public:
T* operator->() { return &myT; }
private:
T myT; }; int main() {
Wrapper<Thing> wThing;
wThing>Foo(); // calls Thing::Foo()
... }
Smart->Pointers*
Smart Pointers
• Introduction
• What are they?
• An example of Smart Pointers
• Benefits of Smart Pointers
• Conclusion
Introductions
In programming, the use of pointers are the main
source of errors (or bugs) when developing code.
The main problem found are the occurrences of
memory leaks, this is due to the way pointers interact
with memory, such as allocation and deallocation,
which when performed inefficiently can cause the
pointer to hang (or dangle, meaning that the pointer
points to a previous removed object).
The solution to this dilemma is the use of Smart Pointers.
What are Smart Pointers?
Smart Pointers look the same as normal
pointers and utilise the same interfacing
operations such as dereferencing (*) and
indirection (->) yet carry other functionality.
Smart pointers reduce bugs and retain efficiency
and control memory management by automated
methods.
What are Smart Pointers?
These automated methods apply to such things as
allocation and deallocation of resources in the effort of
eliminating memory leaks.
template <class T> class auto_ptr
{
T* ptr;
public:
explicit auto_ptr(T* p = 0) : ptr(p) {}
};
~auto_ptr()
{delete ptr;}
T& operator*()
{return *ptr;}
T* operator->()
{return ptr;}
An Example of Smart Pointers
Within the standard C++ library is an example
of a really simple smart pointer
implementation.
This example, auto_ptr, demonstrates
memory management and is found within the
memory header file.
An Example of Smart Pointers
template <class T> class auto_ptr
{
T* ptr;
public:
explicit auto_ptr(T* p = 0) : ptr(p) {}
};
~auto_ptr()
{delete ptr;}
T& operator*()
{return *ptr;}
T* operator->()
{return ptr;}
An Example of Smart Pointers
Therefore instead
of writing…
Therefore user
writes…
void foo()
void foo()
{
{
MyClass* p(new MyClass);
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
p->DoSomething();
delete p;
}
}
An Example of Smart Pointers
Here is an sample of code which illustrates the situation
of a dangling pointer…
MyClass* p(new MyClass);
MyClass* q = p;
delete p;
p->DoSomething();
// Watch out! p is now dangling!
p = NULL;
// p is no longer dangling
q->DoSomething();
// Ouch! q is still dangling!
An Example of Smart Pointers
For auto_ptr, this is solved by setting its pointer to NULL
when it is copied:
template <class T>
auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<T>& rhs)
{
if (this != &rhs)
{
delete ptr;
ptr = rhs.ptr;
rhs.ptr = NULL;
}
return *this;
}
Benefits of Smart Pointers
The main, obvious benefits of using smart pointers as
stated before are the increased efficiency of memory
management over normal pointers.
This efficiency consists of three main abilities of smart
pointers which are, automated initialisation, handling
of dangling pointers and automatic clean up.
Benefits of Smart Pointers
Automated initialisation removes the need for setting
the smart pointer to NULL, which therefore removes
the need for that code to be written, which is similar to
the benefits of automatic clean up in a way.
The automatic clean up feature means that
smart pointers automatically clean up, reduces
the amount of code needed to be written, and in
turn reducing the possibility of bugs.
Benefits of Smart Pointers
Major benefit of the smart pointer is its handling of
dangling pointers. These so called dangling
pointers are a very common problem and are
caused by the pointer pointing to an object that is
already deleted.
Another problem faced by software programmers is
the possibility of an exception error occurring in the
program.
Benefits of Smart Pointers
If an exception occurs within a pointer this means that
the remaining code after it will not get executed and in
turn the pointer will not get deleted with the potential of
memory leaks occurring.
However the use of a smart pointer will remove this
threat due to the automatic clean up of the pointer
because the pointer will be cleaned up whenever it gets
out of scope, whether it was during the normal path of
execution or during an exception.
This solution is possible to write for normal pointers; however it
is much simpler to implement using smart pointers
Conclusion
As previously stated smart pointers are great for
efficient memory management.
They can be used to make more efficient use of
available memory and to shorten allocation and
deallocation time and prevents bugs caused by
standard pointers.
Auto_ptr: the simplest smart pointer to use. For situations
when there are no special requirements.
The End
Download