[C++] Lifetime of Objects Allocated with new

Started by
14 comments, last by Bregma 14 years ago
I am writing a program to improve my C++ skills. It currently has a class that can have child classes of the same type (nesting is not limited). I was using array of pointers to store the child classes:
node::~node (void)
{
	for (unsigned int i = 0; i < nOfSubs; i++)
		delete sub;
	if (sub) {free (sub); sub = 0;}

	if (name) {free (name); name = 0;}
	if (cont) {free (cont); cont = 0;}
}
int node::addSub (wchar_t *name0, wchar_t *cont0, node **addedNode)
{
	sub = (node **) memGet (sub, (nOfSubs + 1) * sizeof (*sub)); //only hand made alloc:malloc (edit: malloc/realloc) switch funtion
	if (!sub)
		return -1;
	sub[nOfSubs] = new node; //note this line!!!
	if (sub[nOfSubs]->set (name0, cont0))
	{
		delete sub[nOfSubs];
		if (nOfSubs)
			sub = (node **) memGet (sub, nOfSubs * sizeof (*sub));
		else
		{
			free (sub);
			sub = 0;
		}
		return -1;
	}
	sub[nOfSubs]->sup = this;
	if (addedNode)
		*addedNode = sub[nOfSubs];
	nOfSubs++;
	return 0;
}

As you can see, I'm using "new" to spawn a new "node". Recently I found this page that suggests that my code should not work, but destructor (node::~node(void)) gets called when I use "delete". Does this work by luck and I'm up to a nasty surprise later or am I misunderstanding the principle of scoping? [Edited by - transistor09 on March 31, 2010 2:35:43 AM]
Advertisement
No, your code looks correct. You delete the objects (not the classes) you new. The destructors of those objects gets invoked at the right time.

Stephen M. Webb
Professional Free Software Developer

A scope is what's between { and }. All the variables created in a scope are destroyed when leaving it, except for those created with the new operator.

So:

{
node n1;
node * n2 = new n2();
} // Here, the n1 destructor will be called, but not n2.

What the page you linked was saying is that when you create an object with new, you must keep its pointer somewhere to be able to call delete on it. For instance, in my example, there's a leak because the pointer on n2 is lost when leaving the scope and there's no way to delete.

I'm not sure to understand everything in the code you provided, but as long as you're creating the object in add and then deleting it in the destructor, it should be fine (unless you keep references to nodes that could be deleted somewhere else in you program).

I'm not sure if my explanation is clear enough.
Oh, right! The variable gets out of scope, not allocated memory! Way to be confusing, Microsoft!

On the other note, will this recursive destructor be a performance killer?
Quote:Original post by transistor09
Way to be confusing, Microsoft!

What has poor old Microsoft to do with that? XD
Quote:Original post by kloffy
What has poor old Microsoft to do with that? XD


Read the original post and maybe you'll find out...
Quote:Original post by Faelenor
Read the original post and maybe you'll find out...

Well, I guess he's referring to the MSDN documents, but I don't see anything wrong or confusing with them, maybe I'm missing something.
Quote:Original post by kloffy
Quote:Original post by Faelenor
Read the original post and maybe you'll find out...

Well, I guess he's referring to the MSDN documents, but I don't see anything wrong or confusing with them, maybe I'm missing something.


You're not. The MSDN document is both clear and correct.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
If youre improving your C++ skills, then why are you mixing free() (and I assume malloc? in getMem) along with new and delete? Probably want to stick to one or the other, unless I over looked something.
Quote:Original post by transistor09
Oh, right! The variable gets out of scope, not allocated memory! Way to be confusing, Microsoft!


It's not confusing at all:

Quote:Objects allocated with the new operator are not destroyed when the scope in which they are defined is exited.


"Objects allocated" == "allocated memory". This doesn't go out of scope. The variable holding the pointer goes out of scope, because that's what variables normally do.

Quote:On the other note, will this recursive destructor be a performance killer?


Well, was it a performance killer to create the nodes in the first place? 'delete' is thought of as faster than 'new', because 'delete' only has to tell the system "this memory is available again", while 'new' has to search for a piece of memory that's big enough. Regardless, it's work that has to be done anyway, so there is no point worrying about how fast it is. The only thing that's at all interesting about it is that you create the nodes one at a time and potentially destroy a bunch of them at once. But it's still not a big issue. Remember, even garbage-collected languages do a full collection over hundreds of megabytes of objects in some few milliseconds. (You can try to make a garbage-collector for C++, but they are not very robust, because the code can't know for sure what is a pointer and what isn't - there are lots of ways to 'hide' a pointer.)

This topic is closed to new replies.

Advertisement