22+-+Polymorphism+and+Bugs

22+-+Polymorphism+and+Bugs - 11/23/10 Containers Templates...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
11/23/10 1 Polymorphism and Bugs EECS 280 Programming and Introductory Data Structures 1 Containers Templates 2 You may have noticed that we declared the BigThing list as: List<BigThing> *lbtp = new List<BigThing>; and NOT : List<BigThing *> *lbtp = new List<BigThing *>; There are several reasons for this and the most important is that there is a fundamental difference between containers-of-objects and containers- of-pointers. Containers-of-pointers are subject to two broad classes of potential bugs: 1. Using an object after it has been deleted 2. Leaving an object orphaned by never deleting it Containers Templates 3 We have introduced the "at-most-once" invariant to avoid the vast majority of these sorts of bugs. At-most-once: a pointer to any specific object can exist in at most one container object at any point in time. 1. Existence : An object must be dynamically allocated before a pointer to it is inserted. 2. Ownership : Once a pointer to an object is inserted, that object becomes the property of the container. No one else may use or modify it in any way. 3. Conservation : When a pointer is removed from a container, either the pointer must be inserted into some container, or it's referent must be deleted. Containers Templates 4 These three rules have an important implication for any method that destroys an existing container. There are (at least) two such methods: 1. The destructor: Destroys an existing instance. 2. The assignment operator: Destroys an existing instance before copying the contents of another instance. Containers Templates 5 Consider the following implementation of the destructor for a singly- linked, simple list, using the interface we've discussed so far: template <class T> List<T>::~List() { while (!isEmpty()) { remove(); } } Question : If this list stores things by pointer , this implementation violates one of the three rules. Which one is violated, and how? int IntList::remove() { node *victim = first; int result; if (isEmpty()) { listIsEmpty e; throw e; } first = victim->next; result = victim->value; delete victim; return result; } // IntList remove() struct node { node *next; node *prev; T *o; }; Yes…I know this is for ints, not Ts. Containers Templates 6 To fix this, we must handle the objects we remove: template <class T> List<T>::~List() { while (!isEmpty()) { T *op = remove(); delete op; } } This provides conservation.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
11/23/10 2 Containers Templates 7 The consequence of this is that the List template must know whether it is something that holds T ’s or “pointers to T ”.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

22+-+Polymorphism+and+Bugs - 11/23/10 Containers Templates...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online