473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

safe deletion of pointer, trees

currently my node is like so with everything public:

class Quadtree_Node
{
public:
Quadtree_Node(Q uadtree_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::Delet e(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 1647

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

class Quadtree_Node
{
public:
Quadtree_Node(Q uadtree_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::Delet e(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::Delet e. The Quadtree::Delet e 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

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

class Quadtree_Node
{
public:
Quadtree_Node(Q uadtree_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::Delet e(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
2498
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 code: Sub Change(ByVal x, ByRef y) x = x+1 y = y+1 End Sub
10
2057
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 these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
3
9630
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 platforms? Also if I delete a pointer twice, and don't nullify the pointer after the first delete, it's going to trap. If I do the nullify, no trap. Does it just confirm that it's safe
26
26218
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 know it's danger to initialize NULL pointers this way. But how about floats? Zhang Le
3
1317
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 one but apparently its not. Kevin
2
2319
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 *int_obj; public: explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
15
2233
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 nodes with the following code, but I don't know how to sum them up and return that value to the calling function in main(). I know the algorithm is recursive in nature, but I get gibberish for
9
2843
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 * nPtr; //the next node's pointer }
3
1537
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 element does not work. After insertion of all the elements into the map, the memory increases from 6600 K to 9600K. But after deleting the elements, the memory decreases from 9600 K to 9400 K. There is no other variable or pointer object used...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10069
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.