17a-Destruction - //d.~Destruction(); // We normally...

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

View Full Document Right Arrow Icon
#include <iostream> using namespace std; class NoDestruction { public: char *name; NoDestruction() { name = new char[20]; cout << "Name? "; cin >> name; } }; void TestNoDestruction() { NoDestruction nd; cout << "Object memory location: " << &nd cout << "name memory location: " << static_cast<void*>(nd.name) << endl; } class Destruction { public: char *name; Destruction() { name = new char[20]; cout << "Name? "; cin >> name; } ~Destruction() { // Calling delete on dynamically allocated memory // causes that memory to be freed back to the system // allowing that memory to be used again // Calling delete on a memory location that is not // valid will cause an error. delete [] name; //name = NULL; } }; void TestDestruction() { Destruction d; cout << "Object memory location: " << &d cout << "name memory location: " << static_cast<void*>(d.name) << endl; } void CallDestructor() { Destruction d; cout << "Object memory location: " << &d cout << "name memory location: " << static_cast<void*>(d.name) << endl;
Background image of page 1

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

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

Unformatted text preview: //d.~Destruction(); // We normally shouldn't be doing this //causes prog to hang } int main (void) { TestNoDestruction(); TestNoDestruction(); TestNoDestruction(); TestDestruction(); TestDestruction(); TestDestruction(); CallDestructor(); return 0; } /* Name? a Object memory location: 0012FE8C name memory location: 00345AF0 Name? s Object memory location: 0012FE8C name memory location: 00345BA8 Name? d Object memory location: 0012FE8C name memory location: 00345BE8 Name? f Object memory location: 0012FE80 name memory location: 00345C28 //destructor called automatically Name? g Object memory location: 0012FE80 name memory location: 00345C28 //destructor called automatically Name? h Object memory location: 0012FE80 name memory location: 00345C28 //destructor called automatically Name? j Object memory location: 0012FE80 name memory location: 00345C28 //destructor called automatically Press any key to continue . . . */...
View Full Document

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Page1 / 2

17a-Destruction - //d.~Destruction(); // We normally...

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

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