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

announcement
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

C++: Smart Pointers Announcement

  • Final exam

– Tuesday, May 20th – 2:45 – 4:45pm – Rm 70-1620

Announcement

  • For those taking SE1 in the fall

– Now a studio course – No need to sign up for lecture and lab – Register for one studio section.

Speaking of Exams

  • There will be no exam this week
  • Next exam:

– May 1st – Topic list to come

Projects

Mandlebrot table on Web is now fixed. deliverable:

– Fractal Image: Due May 2nd

  • Questions?

Lab 7

  • try was broken
  • Now it is fixed
  • Please resubmit Activity 1
slide-2
SLIDE 2

The Plan

  • Today: STL 1
  • Wednesday: STL 2
  • Thursday: Smart Pointers

Memory Leak

  • A bug in a program that prevents it from

freeing up memory that it no longer needs.

  • As a result, the program grabs more and

more memory until it finally crashes because there is no more memory left.

  • In short:

– Allocating without cleaning up.

Pointer Ownership

  • Everything that is a pointer should be
  • wned

– Responsible for cleanup when finished – Should be known to programmer – Should be by design during implementation. – Owner and only owner should perform a delete.

Dangling Pointers

  • Pointer is pointing to something that it

shouldn’t be.

  • Can happen if:

– If the scope of a pointer extends beyond that of the object being pointed to

  • 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 is used in subsequent code.

Getting around these problems

  • The smart pointer

– Prevents memory leaks and dangling pointers – Wrapper class that owns a pointer to an object

The Smart Pointer

Smart pointer ptr

slide-3
SLIDE 3

The Smart Pointer

  • The smart pointer should look and act

like a regular pointer:

– Should support -> – Should support *

  • Should be smarter than your average pointer.

– Reduce memory leaks – Reduce dangling pointers – Take ownership

auto_ptr

  • STL has a standard smart pointer that is a

wrapper around a regular pointer

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;} // ... };

auto_ptr

  • What makes auto_ptr smart is that it takes
  • wnership of the pointer and cleans up

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;} // ... };

Using auto_ptr

Instead of

void foo() { MyClass* p(new MyClass); p->DoSomething(); delete p; }

Use

void foo() { auto_ptr<MyClass> p(new MyClass); p->DoSomething(); }

p will cleanup after itself

Why use auto_ptr?

  • Automatic cleanup

– Reduces memory leaks

  • Automatic initialization

– Includes a default constructor that sets pointer to NULL

  • Avoid dangling pointers

Why use auto_ptr?

  • Dangling pointers

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!

slide-4
SLIDE 4

Why use auto_ptr?

  • Dangling pointers can be avoided by redefining

assignment

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; }

Why use auto_ptr?

  • Other things that can be done during assignment p

= q

– Create a new copy – Ownership transfer

  • auto_ptr use this mechanism

– Reference Counting

  • Pointed object maintains count

– Reference Linking

  • Smart pointer maintains count

– Copy on write

Reference Counts

  • Maintaining the Reference Count

– The reference count will change when:

  • Smart pointer is constructed
  • Smart pointer is copy constructed
  • Smart pointer is assigned to a new SmartPointer.
  • SmartPointer’s destructor is called..

The Smart Pointer

  • Maintaining the Reference Count

– Meaning the smart pointer must define

  • Constructor
  • Copy constructor
  • operator=
  • Destructor

– Note: the smart pointer is a friend to the class of pointers that it is managing. – Questions?

Smart Pointers and STL Containers

  • For efficiency’s sake (this IS C++), all

containers will store their objects by value.

Smart Pointers and STL Containers

  • Funny thing about C++ Inheritance

– You can only gain polymorphic behavior on pointers (or references) to objects an not on

  • bjects themselves.

Vehicle V = Car (); // not allowed Vehicle *V = new Car (); // okay

slide-5
SLIDE 5

Smart Pointers and STL Containers

class Base { /*...*/ }; class Derived : public Base { /*...*/ }; Base b; Derived d; vector<Base> v; v.push_back(b); // OK v.push_back(d); // error

Smart Pointers and STL Containers

vector<Base*> v; v.push_back(new Base); // OK v.push_back(new Derived); // OK too // cleanup: you must do explicitly! for (vector<Base*>::iterator i = v.begin(); i != v.end(); ++i) delete *i;

Smart Pointers and STL Containers

  • Using smart pointers:

vector< linked_ptr<Base> > v; v.push_back(new Base); // OK v.push_back(new Derived); // OK too // cleanup is automatic

Smart Pointers and STL Containers

  • Another problem with maintaining maps of

pointers to objects.

– These STL classes will compare the items placed in them

  • Meaning that actual pointer values (memory

addresses) will be compared

  • What we would like instead is to compare the
  • bjects being pointed to.
  • Smart pointers can be extended to provide this.

– How? By redefining operator<

Smart Pointers and STL Containers

  • Warning:

– 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

Smart Pointers and STL Containers

  • Questions?
slide-6
SLIDE 6

Smart Pointers

  • How can I get them

– The only Standard smart pointer (STL) is the auto_ptr

  • Copies are not equivalent
  • Uses ownership method on copy

– Other smart pointers (reference counting) are available from Web page previously referenced.

Summary

  • Smart Pointers

– Wrapper class on a pointer – Standard smart pointer: auto_ptr – Takes ownership of pointer

  • Reduce memory leaks
  • Reduce dangling pointer

– Comparison operator for use in STL classes.

  • Use with caution
  • Questions?

– Have a nice weekend.