A
Antimon
Hi,
As a c++ newbie, i'm trying to figure out stuff mysself and i have a
problem, thougt someone could help.
Here's the code:
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test();
};
Test::Test()
{
}
void test()
{
vector<Test*> v;
for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}
for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}
int main()
{
for (int i = 0; i < 10; i++)
test();
string a;
cin >> a;
return 0;
}
Since i delete the pointers in my vector i thought there's no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)
Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?
Thanks.
As a c++ newbie, i'm trying to figure out stuff mysself and i have a
problem, thougt someone could help.
Here's the code:
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test();
};
Test::Test()
{
}
void test()
{
vector<Test*> v;
for (int i = 0; i < 5000000; i++)
{
v.push_back(new Test());
}
for (vector<Test*>::iterator i = v.begin(); i != v.end(); i++)
delete *i;
}
int main()
{
for (int i = 0; i < 10; i++)
test();
string a;
cin >> a;
return 0;
}
Since i delete the pointers in my vector i thought there's no memory
problems with this code. But when i compile it using visual stuido
2005, it ends up using ~25mb memory at the end of that loop. So i tried
it with gcc on cygwin and it worked just fine, final mem usage: 900kb.
(peaks to 100mb both ways)
Am i missing something? Can that code leak memory when it runs without
any exceptions before cleanup code?
Thanks.