I have a problem and I have been trying to solve it for quite a while
I use a simple std::vector to store pointers to objects. But when I want to delete the objects I have to much ram usage left over.
When I start the programm it uses 328 K ram. When the objects are created it uses 2.448 K and when I delete everything I have 1.128 K left over with method 1 and 1.108 K left with method 2.
I have researched about how to delete a vector and the methods should work :/ This is the code stripped down to the problem:
Code: Select all
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class someTestClass
{
private:
double someData;
public:
someTestClass()
{
someData = rand()/2.0;
}
~someTestClass()
{
}
};
int main()
{
vector<someTestClass*> vector_data;
cout<<"creating\n";
while(vector_data.size()<100000)
{
someTestClass *temp = new someTestClass();
vector_data.push_back(temp);
}
cout<<"deleting\n";
//Of course both methods do not work together, so comment or uncomment them
///Method 1
vector<someTestClass*>::iterator it_knoten = vector_data.begin();
while(it_knoten!=vector_data.end())
{
someTestClass *tmp = (*it_knoten);
delete tmp;
tmp=0;
it_knoten = vector_data.erase(it_knoten);
}
///Method 2
while(!vector_data.empty())
{
delete vector_data.back();
vector_data.pop_back();
}
cout<<"end\n";
cin.get();
return 0;
}
I hope you know whats wrong with it
greetings