announcement
play

Announcement Final exam C++: Smart Pointers Tuesday, May 20 th - PDF document

Announcement Final exam C++: Smart Pointers Tuesday, May 20 th 2:45 4:45pm Rm 70-1620 Announcement Speaking of Exams For those taking SE1 in the fall There will be no exam this week Now a studio course


  1. Announcement • Final exam C++: Smart Pointers – Tuesday, May 20 th – 2:45 – 4:45pm – Rm 70-1620 Announcement Speaking of Exams • For those taking SE1 in the fall • There will be no exam this week – Now a studio course • Next exam: – No need to sign up for lecture and lab – May 1 st – Register for one studio section. – Topic list to come Projects Lab 7 Mandlebrot table on Web is now fixed. • try was broken • Now it is fixed deliverable: – Fractal Image: Due May 2 nd • Please resubmit Activity 1 • Questions?

  2. The Plan Memory Leak • Today: STL 1 • A bug in a program that prevents it from freeing up memory that it no longer needs. • Wednesday: STL 2 • As a result, the program grabs more and • Thursday: Smart Pointers more memory until it finally crashes because there is no more memory left. • In short: – Allocating without cleaning up. Pointer Ownership Dangling Pointers • Pointer is pointing to something that it • Everything that is a pointer should be shouldn’t be. owned • Can happen if: – Responsible for cleanup when finished – If the scope of a pointer extends beyond that of – Should be known to programmer the object being pointed to – Should be by design during implementation. • i.e Returning a pointer to a local variable. – If a dynamically allocated memory cell is freed explicitly and then the pointer pointing to such a space – Owner and only owner should perform a delete. is used in subsequent code. Getting around these problems The Smart Pointer • The smart pointer Smart pointer – Prevents memory leaks and dangling pointers – Wrapper class that owns a pointer to an object ptr

  3. The Smart Pointer auto_ptr • STL has a standard smart pointer that is a • The smart pointer should look and act wrapper around a regular pointer like a regular pointer: – Should support -> template <class T> class auto_ptr – Should support * { • Should be smarter than your average pointer. T* ptr; – Reduce memory leaks public: explicit auto_ptr (T* p = 0) : ptr(p) {} – Reduce dangling pointers ~auto_ptr () {delete ptr;} – Take ownership T& operator* () {return *ptr;} T* operator-> () {return ptr;} // ... }; auto_ptr Using auto_ptr • What makes auto_ptr smart is that it takes Use Instead of ownership of the pointer and cleans up template <class T> void foo () void foo () class auto_ptr { { { auto_ptr < MyClass > MyClass * p(new MyClass ); T* ptr; p(new MyClass ); p-> DoSomething (); p-> DoSomething (); public: delete p; } explicit auto_ptr (T* p = 0) : ptr(p) {} } ~auto_ptr () {delete ptr;} T& operator* () {return *ptr;} p will cleanup after itself T* operator-> () {return ptr;} // ... }; Why use auto_ptr? Why use auto_ptr? • Dangling pointers • Automatic cleanup – Reduces memory leaks MyClass * p(new MyClass ); • Automatic initialization MyClass * q = p; – Includes a default constructor that sets pointer delete p; to NULL p-> DoSomething (); // Watch out! p is now dangling! p = NULL; // p is no longer dangling • Avoid dangling pointers q-> DoSomething (); // Ouch! q is still dangling!

  4. Why use auto_ptr? Why use auto_ptr? • Dangling pointers can be avoided by redefining • Other things that can be done during assignment p assignment = q – Create a new copy template <class T> – Ownership transfer auto_ptr <T>& auto_ptr <T>:: operator= ( auto_ptr <T>& rhs) • auto_ptr use this mechanism { – Reference Counting if (this != &rhs) { • Pointed object maintains count delete ptr; – Reference Linking ptr = rhs.ptr; • Smart pointer maintains count rhs.ptr = NULL; – Copy on write } return *this; } Reference Counts The Smart Pointer • Maintaining the Reference Count • Maintaining the Reference Count – Meaning the smart pointer must define – The reference count will change when: • Constructor • Smart pointer is constructed • Copy constructor • operator= • Smart pointer is copy constructed • Destructor • Smart pointer is assigned to a new SmartPointer. • SmartPointer’s destructor is called.. – Note: the smart pointer is a friend to the class of pointers that it is managing. – Questions? Smart Pointers and STL Containers Smart Pointers and STL Containers • For efficiency’s sake (this IS C++), all • Funny thing about C++ Inheritance containers will store their objects by value. – You can only gain polymorphic behavior on pointers (or references) to objects an not on objects themselves. Vehicle V = Car (); // not allowed Vehicle *V = new Car (); // okay

  5. Smart Pointers and STL Containers Smart Pointers and STL Containers class Base { /*...*/ }; vector < Base *> v; class Derived : public Base { /*...*/ }; v. push_back (new Base ); // OK v. push_back (new Derived ); // OK too Base b; Derived d; // cleanup: you must do explicitly! for ( vector < Base *>:: iterator i = v. begin (); i != v. end (); ++i) vector < Base > v; delete *i; v. push_back (b); // OK v. push_back (d); // error Smart Pointers and STL Containers Smart Pointers and STL Containers • Another problem with maintaining maps of • Using smart pointers: pointers to objects. – These STL classes will compare the items vector < linked_ptr < Base > > v; placed in them v. push_back (new Base ); // OK • Meaning that actual pointer values (memory v. push_back (new Derived ); // OK too addresses) will be compared • What we would like instead is to compare the // cleanup is automatic objects being pointed to. • Smart pointers can be extended to provide this. – How? By redefining operator< Smart Pointers and STL Containers Smart Pointers and STL Containers • Warning: • Questions? – STL containers may copy and delete their elements behind the scenes – all copies of an element must be equivalent – Should only use reference counting smart pointers with STL containers

  6. Smart Pointers Summary • Smart Pointers • How can I get them – Wrapper class on a pointer – The only Standard smart pointer (STL) is the – Standard smart pointer: auto_ptr auto_ptr – Takes ownership of pointer • Copies are not equivalent • Reduce memory leaks • Uses ownership method on copy • Reduce dangling pointer – Comparison operator for use in STL classes. – Other smart pointers (reference counting) are • Use with caution available from Web page previously referenced. • Questions? – Have a nice weekend.

Recommend


More recommend