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

does vector<>.resize call delete?

P: n/a
Hi, when there is a vector<> of pointers to some objects, does calling
resize cause vector to call delete on each object, or is there a memory leak
problem?

for example:

struct base {//some vars; ~base();};
vector<base*> vb;
vb.push_back(new base);
vb.push_back(new base);
vb.push_back(new base);
vb.push_back(new base);

vb.resize(12); // cause memory leak here?

Thanks in advance.

S
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"john smith" <as**@asdf123asdf.net> wrote in message
news:bh***********@news.eecs.umich.edu...
Hi, when there is a vector<> of pointers to some objects, does calling
resize cause vector to call delete on each object, or is there a memory leak problem?

for example:

struct base {//some vars; ~base();};
vector<base*> vb;
vb.push_back(new base);
vb.push_back(new base);
vb.push_back(new base);
vb.push_back(new base);

vb.resize(12); // cause memory leak here?

Thanks in advance.

S

You chose a bad example. But vector will *never* call delete on the pointers
you put into it. Think about it, how could vector know that you allocated
the pointers using new? All pointers look the same whether they've been
allocated with new, malloc or not allocated at all.

This is why a vector of pointers is a bad idea (in general). Learn about
smart pointers instead.

john
Jul 19 '05 #2

P: n/a
Victor Bazarov wrote:

'resize' will copy the first four elements and then create and
default-initialise (zero-initialise, in case of pointers) eight
more objects.


Victor, is that the right terminology? I believe Accelerated C++ uses
the term "value-initialize" for what happens it this case.
"default-initialization" would leave built-in types undefined (unless
they are static). Or am I mistaken?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.