Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()...

Similar presentations


Presentation on theme: "Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()..."— Presentation transcript:

1 template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()... } Smart - > Pointers * BY: GLENN WHEELER AND MOHAMED SHAIKH

2 Smart Pointers Introduction What are they? An example of Smart Pointers Benefits of Smart Pointers Conclusion

3 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.

4 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.

5 These automated methods apply to such things as allocation and deallocation of resources in the effort of eliminating memory leaks. What are Smart Pointers? template 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;} };

6 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.

7 template 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

8 Therefore instead of writing… void foo() { MyClass* p(new MyClass); p->DoSomething(); delete p; } Therefore user writes… void foo() { auto_ptr p(new MyClass); p->DoSomething(); }

9 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!

10 An Example of Smart Pointers For auto_ptr, this is solved by setting its pointer to NULL when it is copied: template auto_ptr & auto_ptr ::operator=(auto_ptr & rhs) { if (this != &rhs) { delete ptr; ptr = rhs.ptr; rhs.ptr = NULL; } return *this; }

11 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.

12 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. Benefits of Smart Pointers 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.

13 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.

14 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

15 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.

16 The End


Download ppt "Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()..."

Similar presentations


Ads by Google