destructors-1/prog.cc - A sample program with memory leaks. | Lecture 4 - slide 12 : 40 Program 3 |
#include <iostream> #include "point.h" using namespace std; int f(){ Point p, q, r(11.0, 12.0), ap[2], *s = new Point(13.0, 14.0); cout << "Point p: " << p << endl; // (0,0) cout << "Point q: " << q << endl; // (0,0) cout << "Point r: " << r << endl; // (11,12) cout << "Point ap[0]: " << ap[0] << endl; // (0,0) cout << "Point ap[1]: " << ap[1] << endl; // (0,0) cout << "Point *s: " << *s << endl; // (13,14) // MEMORY LEAKS HERE: // The Point destructor is called 5 times, for p, q, r, ap[0] and ap[1], // but the calls fail to release the Point resources: // The dynamically allocated memory for p, q, r, ap[0] and ap[1] is never deallocated. // In addition: The point pointed to by s (together with its representation) is never deallocated: // No destructor is called for s. } int main(){ f(); }