Lecture12-3 - 1 Data Structures CSCI 132, Spring 2012...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Data Structures CSCI 132, Spring 2012 Lecture 12 Destructors, Copy Constructors and Overloading 2 Stacks as Linked lists top entry middle entry bottom entry top_node typedef Stack_entry Node_entry; class Stack { public: Stack( ); bool empty( ); Error_code push( const Stack_entry &item); Error_code pop( ); Error_code top(Stack_entry &item) const; protected: Node *top_node; } 3 Clients creating garbage for (int i = 0; i < 1000000; i++) { Stack small; small.push(some_data); } Problem: small exists only within for loop. It is created repeatedly (1000000 times!) and goes out of scope outside of loop. Memory is allocated, but not freed up. Solution: Use a Destructor function 4 The Stack destructor Stack :: ~Stack( ) { while (!empty( ) ) { pop( ); //pop( ) deletes nodes } } Destructors are called automatically when objects go out of scope. Destructor is indicated with the tilda. 5 Default Assignment Stack outer_stack; for (int i =0; i <1000000; i++){ Stack inner_stack;...
View Full Document

This document was uploaded on 03/09/2012.

Page1 / 12

Lecture12-3 - 1 Data Structures CSCI 132, Spring 2012...

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

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