473,378 Members | 1,388 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,378 software developers and data experts.

safe deletion of pointer, trees

currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}

How can I change this so that I can make my data members of class
Quadtree_Node private? I was thinking of making a GetFunction to get the
pointers to the children of the node, but then I am not sure if I would be
returning a copy of the pointer, a referance of the pointer or the pointer
itself for use in the deletion.

A try:

const Quadtree_Node *& GetTopLeft() {return m_tlchild;}
delete ->node->GetTopLeft();

Would that be safe?

Thanks,
Christopher


Jul 22 '05 #1
2 1625

"Christopher Pisz" <so*****@somewhere.com> wrote in message
news:ch**********@news.swt.edu...
currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}


You should, in general, allow the object being deleted to handle deleting
its own members. So, you should simply call delete on node itself, and
allow the destructor for the Quadtree_Node to delete all its members. Then,
the members can be public, protected, or private, as you desire.

Unless there is more code you haven't shown in the Delete function, you can
simply remove that, and call delete directly on the node you were previously
passing to Quadtree::Delete. The Quadtree::Delete function, as it is shown
above, is simply not needed.

You'll also probably want an assignment operator for your Quadtree_Node
class. (Google for the "rule of three" to see why.)

-Howard

Jul 22 '05 #2

"Christopher Pisz" <so*****@somewhere.com> wrote in message
news:ch**********@news.swt.edu...
currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds);
~Quadtree_Node();
void Subdivide(float smallest_node);
const Quadtree_Node *& GetTopLeft();
Bounding_Box m_bounds; // Bounding Coordinates of this node
Quadtree_Node * m_parent; // Parent of this node, NULL if root
Quadtree_Node * m_tlchild; // Top Left child node
Quadtree_Node * m_trchild; // Top Right child node
Quadtree_Node * m_blchild; // Bottom Left child node
Quadtree_Node * m_brchild; // Bottom Right child node
std::list<Data> m_data; // Linked List of Data
};

And my deletion method looks like:

void Quadtree::Delete(Quadtree_Node * node)
{
if(node)
{
Delete(node->m_tlchild);
Delete(node->m_trchild);
Delete(node->m_blchild);
Delete(node->m_brchild);
}
delete node;
}

How can I change this so that I can make my data members of class
Quadtree_Node private? I was thinking of making a GetFunction to get the
pointers to the children of the node, but then I am not sure if I would be
returning a copy of the pointer, a referance of the pointer or the pointer
itself for use in the deletion.


There is no reason in the above code that you can't just make them private
as it is. I mean what's the problem, Delete is a member of Quadtree so it is
allowed to access private members of Quadtree, even if those are members of
a different node. Try it and see.

john
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
10
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of...
3
by: Songling | last post by:
Hi gurus, Just learnt from my co-worker that it's safe to delete a null pointer. I tried it on sun box (and linux), nothing bad happens. So it seems to be true. But does it apply to all...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
3
by: Kevin C | last post by:
Is there a safe Application log event source? By safe, I mean one that will always exist (excluding any manual deletion) on any and all normal xp, 2003 installations. I thought "Application" was...
2
by: Kuba_O | last post by:
Hello, i've got simple question about std::auto_ptr: what makes it is exceptions safe? Lets say i have class "int_smart_ptr" implemented like this: class int_smart_ptr { private: int...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
3
by: Yuvanya | last post by:
Hi, I have a map that stores pointer objects. I am not able to delete these pointer objects from the map. For loop that gets each element of the map from begin() to end() and deletes that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.