Dynamic memory - create multiple instances without explicitly call delete
Mar 28, 2014 at 4:33pm Mar 28, 2014 at 4:33pm UTC
Hi,
Please look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
using namespace std;
class teste
{
public :
teste(){x=33;};
~teste(){cout << x << endl;};
int x;
};
int main()
{
teste *x;
for (int i=0;i<10;i++)
x = new teste();
return 0;
}
Each time i call "x = new teste();" the previous object is deleted?
Does this code cause any sort of memory leak?
Mar 28, 2014 at 4:46pm Mar 28, 2014 at 4:46pm UTC
Each time i call "x = new teste();" the previous object is deleted?
No. If you 'new' something, you have to 'delete' it or else it never gets cleaned up.
Every 'new' must have a matching 'delete'
Does this code cause any sort of memory leak?
Yes.
Topic archived. No new replies allowed.