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

anonymous pointer creation and memory leaks

P: n/a
I have a quick question for you STL gurus out there. Suppose I do
something like this:

vector <myClass*> foo;
foo.push_back(new myClass());

Does this work in C++? I know you can do it in C# and Java. Also, if
you call foo.clear(), are the objects deleted properly (any memory
leaks doing this kind of thing)?

Thanks for your input!

DR

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
dr*******@yahoo.com wrote:
I have a quick question for you STL gurus out there. Suppose I do
something like this:

vector <myClass*> foo;
foo.push_back(new myClass());

Does this work in C++?
If myClass is a type, yes. Why shouldn't it?
I know you can do it in C# and Java.
C# and Java don't have pointers.
Also, if you call foo.clear(), are the objects deleted properly (any
memory leaks doing this kind of thing)?


Depends on what you mean by "the objects". You store pointers in your
vector, and those pointers will be destroyed. The objects that they point
to, however, won't. How would the vector know whether you want them to be
deleted or not? You have to delete them yourself.

Jul 23 '05 #2

P: n/a
You're right, C# and Java don't have pointers, but internally
everything is represented as dynamic references and are garbage
collected when the garbage collected gets around to it. Really,
besides the point.

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?

DR

Jul 23 '05 #3

P: n/a
dr*******@yahoo.com schrieb:
You're right, C# and Java don't have pointers, but internally
everything is represented as dynamic references and are garbage
collected when the garbage collected gets around to it. Really,
besides the point.

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?


Yes. Or better yet avoid filling the vector with pointers unless you
have a good reason to do so. The main reasons that come to mind are
polymorhic or noncopyable/expensive to copy elements. In all of those
cases, smart pointers take the burden of deletion off you.

Cheers,
Malte
Jul 23 '05 #4

P: n/a

"Malte Starostik" <ma***@starostik.de> wrote in message
news:42********@olaf.komtel.net...
dr*******@yahoo.com schrieb:
You're right, C# and Java don't have pointers, but internally
everything is represented as dynamic references and are garbage
collected when the garbage collected gets around to it. Really,
besides the point.

But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?


Yes. Or better yet avoid filling the vector with pointers unless you
have a good reason to do so. The main reasons that come to mind are
polymorhic or noncopyable/expensive to copy elements. In all of those
cases, smart pointers take the burden of deletion off you.


To be explicit for C-Sharpies and Javites, when polymorphism is not
required:

struct myClass{};

void somefn()
{
std::vector<myClass> foo;

foo.push_back( myClass() );
}

All memory management is handled automatically.

Jeff Flinn
Jul 23 '05 #5

P: n/a
dr*******@yahoo.com wrote:
But what you're saying is that if I have a vector and I call clear() to
clear them out (or if I want to remove one or several manually), I
should first delete any objects that are associated with those pointers
in that collection. Do I have it right?


the issue is of 'ownership'. the container doesn't know if you have the
pointer store elsewhere too (by the time you say foo.push_back(), the
pointer is already created as if you had done:

x* y = new x();
foo.push_back(y);

so your code could have easily been:
x* y = new x();
fooAnother.push_back(y);
foo.push_back(y);

therefore, foo doing a delete on the object that's pointed to would be
disastrous. to get a little closer to the behavior you get in java/C#
(although i don't know much about C# at all), look at boost::shared_ptr.
(http://www.boost.org/libs/smart_ptr/smart_ptr.htm)

be careful though, shared_ptr alone will get you in trouble if you have
circular pointers. (this used to be a problem in JVM gc until they
implemented mark-and-sweep way back, i think). to be
circular-reference-proof, you'd have to use a combination of shared_ptr and
weak_ptr. it's all discussed in the URL above.

cheers,
anoop aryal
Jul 23 '05 #6

P: n/a
I appreciate all of your feedback! That makes sense and is probably
something that I should know already :) I'd have to say that those of
us who use Java/C# are spoiled to garbage collection (in all of it's
glory).

Thanks again all!

DR

Jul 23 '05 #7

P: n/a

"Rolf Magnus" <ra******@t-online.de> skrev i en meddelelse
news:d2*************@news.t-online.com...
dr*******@yahoo.com wrote:
I have a quick question for you STL gurus out there. Suppose I do
something like this:

vector <myClass*> foo;
foo.push_back(new myClass());

Does this work in C++?


If myClass is a type, yes. Why shouldn't it?


It does work, but there is a slight risc of a leak - namely if push_back
should throw.

[snip]
/Peter
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.