Vector of pointers

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.
 
A

Andre Kostur

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?

Yep. The memory that you (correctly) delete doesn't necessarily get
handed back to the operating system. The C++ runtime on whatever
platform you're on may be doing some sort of memory caching... not giving
the memory back to the OS (typically a "slow" operation, YMMV) but
holding on to it to allocate it back into the program when requested.
 
S

Salt_Peter

Antimon said:
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?

Nope, you haven't missed anything. The language does not impose a
requirement over when the allocated memory is returned to the heap by
the platform. Thats implementation defined.

I'ld suggest a try-catch block in case a std::bad_alloc is thrown.

int main()
{
try
{
// do stuff
}
catch( const std::exception& e )
{
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}
}
 
A

Antimon

Thanks alot for helping, just checked it and yes when something else
needs memory, it starts to give that memory back to os.
 
R

red floyd

Antimon wrote:
[top posting redacted]


Hi and welcome! Glad the group was able to answer your question.

Just a recommendation for future posting, it's considered poor etiquette
to top post (i.e. reply *above* the text you're responding to) in this
group.

You should edit the text you're replying to, including stripping
signatures, and intersperse your comments with the text.

Example:

Antimon wronte:
> something red floyd wants to respond to

red floyd's response.
> something else red floyd wants to talk about

red floyd's response.

etc...

Hope this helps, don't want you to get flamed for netiquette issues,
when you're looking for help.
 
F

Frank Puck

Antimon said:
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?


the allocator for vector may be keeping the allocated memory.
You could write your own allocator to avoid this.
 
B

BobR

Antimon wrote in message
Thanks a lot for helping, just checked it and yes when something else
needs memory, it starts to give that memory back to os.

What red floyd said. (dang, red, I've never seen a slap-in-the-wrist put so
nicely! ).

See below...

// Try this here and see what your results are.
// [ just out of curiosity ]

static bool stopper( true );
if( stopper ){ // so it doesn't print 10 times.
cout<<" v.size()="<<v.size()<<std::endl;
cout<<" v.capacity()="<<v.capacity()<<std::endl;
}

vector<Test*>().swap( v );

if( stopper ){ // so it doesn't print 10 times.
cout<<" v.size()="<<v.size()<<std::endl;
cout<<" v.capacity()="<<v.capacity()<<std::endl;
stopper = false;
}

Memory change, same?
 
R

red floyd

BobR said:
What red floyd said. (dang, red, I've never seen a slap-in-the-wrist put so
nicely! ).

Thanks. I think :)

Wasn't really trying for the slap-on-the-wrist effect; I was aiming more
for the "arm around the shoulder, friendly advice" effect.
 
A

Antimon

red said:
Hi and welcome! Glad the group was able to answer your question.

Thanks alot.
Just a recommendation for future posting, it's considered poor etiquette
to top post (i.e. reply *above* the text you're responding to) in this
group.

You should edit the text you're replying to, including stripping
signatures, and intersperse your comments with the text.

And thanks for the advice too. I'm not used to newsgroups much but
gonna learn :)

Take care..
 
A

Antimon

BobR said:
vector<Test*>().swap( v );

I tried this, yes it trims the vector to 0 size but it doesnt release
that memory.

Output:
v.size()=5000000
v.capacity()=5314959
v.size()=0
v.capacity()=0
Memory change, same?

Same, but when systems runs out of memory somehow, it starts to
deallocate memory (drop from 25mb mem usage to 600kb) caching seems
fine but 25mb is a huge amount to just cache :)

Btw, i tried this with mingw too, it is just like visual c++. Only gcc
on cygwin releases all memory allocated after test() calls.

Thanks.
 
B

Bo Persson

Antimon said:
I tried this, yes it trims the vector to 0 size but it doesnt
release that memory.

But it makes it available for other allocations in your program.
Output:
v.size()=5000000
v.capacity()=5314959
v.size()=0
v.capacity()=0


Same, but when systems runs out of memory somehow, it starts to
deallocate memory (drop from 25mb mem usage to 600kb) caching seems
fine but 25mb is a huge amount to just cache :)

Btw, i tried this with mingw too, it is just like visual c++. Only
gcc on cygwin releases all memory allocated after test() calls.

Thanks.

On a system with a lot of free memory, why bother to clean up after
yourself? Just grab the free memory you need, as long as there is nobody
else around wanting it.

The Windows Task Manager reports the current working set, not how much of
that memory is actually in use. It is notoriuos for reporting huge memory
usage on a lightly loaded system. As soon as other programs make demands for
memory, Windows will reclaim the excess from your program.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top