In this code I noticed that the memory stays used when i press ctrl alt delete:
Code:
#include <vector>
#include <iostream>
using namespace std;
class someClass
{
public:
someClass(double ix, double iy);
~someClass();
private:
double x, y;
};
someClass::someClass(double ix, double iy)
{
x = ix;
y = iy;
}
someClass::~someClass() {}
int function()
{
vector<someClass*> someClassList;
double x = 0;
double y = 0;
int count;
while (count < 500000)
{
someClassList.push_back(new someClass(x, y));
x = x + .01;
y = y + .01;
count++;
}
/* USE someClassList here */
// Delete it here but how?
return 0;
}
int main()
{
cin.get();
for (int i = 0; i < 100; i++)
function();
cin.get();
return 0;
}
I know I must delete the someClass pointers in the list but how? From using the search function of this board and finding a simular problem with a solution I have found and modified "int function()" to this:
Code:
int function()
{
vector<someClass*> someClassList;
double x = 0;
double y = 0;
int count;
while (count < 500000)
{
someClassList.push_back(new someClass(x, y));
x = x + .01;
y = y + .01;
count++;
}
/* New part */
vector<someClass*>::iterator I = someClassList.begin();
while (I != someClassList.end())
{
delete *I;
I++;
}
return 0;
}
Is this the corrent way to free the memory? Thank you for your time.