Now we have to create class X
(as base class) and class Y
(as derived). They both uses integer pointers to hold different size of allocations.
class X
{
int *p;
public:
X() {p=new int[2];}
~X() {delete[] p;}
};
class Y : public X {
int *q;
public:
Y() {q=new int[4];}
~Y() {delete[] q;}
};
And let's try it in a simple loop:
for(int i=0;i<8;i++){X *ptr =new Y; delete ptr;}
What causes the memory leak there and what can we do to fix it?