By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

Vector of pointers

P: n/a
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.

Nov 20 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"Antimon" <an*****@gmail.comwrote in news:1164060480.203230.172480
@j44g2000cwa.googlegroups.com:
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.
Nov 20 '06 #2

P: n/a

Antimon wrote:
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;
}
}

Nov 20 '06 #3

P: n/a
Thanks alot for helping, just checked it and yes when something else
needs memory, it starts to give that memory back to os.
On Nov 21, 2:02 am, "Salt_Peter" <pj_h...@yahoo.comwrote:
Antimon wrote:
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;
}

}- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Nov 20 '06 #4

P: n/a
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.
Nov 21 '06 #5

P: n/a

"Antimon" <an*****@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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.

Nov 21 '06 #6

P: n/a

Antimon wrote in message
<11**********************@m7g2000cwm.googlegroups. com>...
>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...
>Antimon wrote:
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;
// 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;
}
}
int main(){
try{
for (int i = 0; i < 10; i++)
test();
}
catch( const std::exception& e ){
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}
string a;
cin >a;
return 0;
}
Memory change, same?

--
Bob R
POVrookie
Nov 21 '06 #7

P: n/a
BobR wrote:
>
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.

Nov 21 '06 #8

P: n/a
red floyd wrote:
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..

Nov 21 '06 #9

P: n/a
BobR wrote:
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.

Nov 21 '06 #10

P: n/a
Antimon wrote:
BobR wrote:
>vector<Test*>().swap( v );

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
>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.
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
Nov 21 '06 #11

P: n/a

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

Thanks. I think :-)
Yup. It was meant as a "Good job!".

--
Bob R
POVrookie
Nov 21 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.