开发者

Pointer scope in C++

Although this is a very general question, here's my specific example so you can underst开发者_开发问答and what I'm asking.

I have a copy constructor for a quad-tree class. So I write a recursive helper method called copy so I can call it in my copy constructor. The helper takes one parameter, a node. The root node is given as the first argument when called from the constructor.

So now in my helper method, I create one new node call it newNode, and each node has member variables neChild, nwChild, seChild, swChild----sw means southwest nw, ne, se are all cardinal directions, and I set each new child equal to that of source.child. Then I call the helper method again recursively on each source.child (4 of them). So this way, 1 node becomes 4, which then has 16 childs, and so on. And so then I return that first newNode I created.

Question:

Will that new returned node have all the other nodes attached to it? Will it be in a tree structure (although not formally a tree)? Or will those pointers attached to returned pointers go out of scope?


You will end up with a new root node which is linked to the same child nodes as the original tree. So any changes made to any children will affect both trees, and you may have a double-free problem later.

This is because, although you're calling copy recursively, you're discarding the return value.

Furthermore, you said this copy function is called from your copy constructor? You're in for a world of hurt, because copy calls the copy constructor (at the return statement, since you return by value). That's a recipe for a STACK OVERFLOW. I guess you're in the right place ;)


First, as others have pointed out, you are confusing QuadtreeNode and QuadtreeNode* when you create newNode. I'll assume it's a pointer.

Second, you're not guarding against NULLs well enough.

Third, if my reading of this code is correct, the four recursive calls to copy produce nothing but memory leaks; each call generates a new node on the heap, and returns a pointer to that new node, but your code does not retain that pointer. So copy will return a pointer to a new node, which has child pointers pointing to the children of the argument node-- and that's all.


Assuming you are returning QuadtreeNode* from your copy method, the pointer you are returning from this function will remain valid outside this function until you manually release the memory using delete.


If newNode type is QuadtreeNode*, then the way to access the member variables/methods is with -> operator.

newNode lies on stack but the memory it is pointing to lies on heap( And this where the object lies in this case ). So, it is returning a reference to the location on the heap( If return type is QuadtreeNode*). Its fine. But, the program needs to deallocate the resources acquired from the heap/free store using delete, else memory leak prevails.


Stuff allocated from the heap will not go out of scope on function return. A local variable pointing to something on the heap would go out of scope but, if you're returning it from the function, then you're keeping a copy.

If, however, those variables are automatic (on the stack), they will go out of scope when you return.

And, based on your code, which was something like (not posting actual code since OP stated it was homework and they didn't want copies kept):

XTreeNode XTreeNode::copy(XTreeNode & const n) {
    XTreeNode *newNode = new XTreeNode(7);  // <-- Note "*", this is important.
    newNode.xy=n.xy;
    return newNode;
}

You have chosen the third of those two options I gave :-)

Because you are passing in n, it will still be in scope when you exit the function and the one thing that does go out of scope (newNode) is being returned anyway, so you will still have a copy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜