![]() ![]() ![]() ![]() | A sample program with memory leaks. | Lecture 3 - slide 10 : 36 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 point representation of p, q, r, ap[0] and ap[1] are 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(); }