473,498 Members | 310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector of pointers

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
11 5001
"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

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

"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2949
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
3503
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
13
2123
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
11
2714
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
2295
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
3601
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
5
5457
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
3233
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
3975
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
3490
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.