I am trying to understand the memory leak. I don't understand why below MyInt deconstruct in main is never called and a memory leak happens.
class MyInt
{
int *_p;
public:
MyInt(int *p = NULL) { _p = p; }
~MyInt()
{
delete _p;
}
int &operator*() { return *_p; }
};
int main()
{
double t[] = {1.0, 2.0, 3.0, 4.0, 5.0};
for (size_t i = 0; i < 5; ++i)
{
MyInt *en = new MyInt(new int(i));
std::cout << **en << "/" << t[i] << " = " << **en / t[i] << std::endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire