473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

anonymous pointer creation and memory leaks

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

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

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

Similar topics

19
by: Sergey Koveshnikov | last post by:
Hello, If my function return a pointer on a memory area, where do I free it? e.x.: char *foo() { char *p; p = malloc(10); strcpy(p, "something"); return(p); }
2
by: MJL | last post by:
I am working on a small project that involves the manipulation of dynamically allocated memory for strings. I was wondering why the string.h functions are the way they are and why not as follows:...
3
by: anonymous | last post by:
I believe I ran into an interesting way to create memory leaks in C# 2.0 using anymous delegates. Here is a sample of the code in question. private void Handle_Event(object sender, EventArgs e)...
13
by: morz | last post by:
My question is,this code is valid or not? can this code cause memory leaks? #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv) {
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.