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

deletes and more deletes

63
Hello, I have crated a binary tree template for school. And in my program implements this data structure, I have to have two trees, both of which are pointing to the same data, just in different orders.

The problem is, my template takes care of deleting the information, so when I delete one node, the pointer in the other tree is now pointing to unallocated memory. So I guess my question comes down to, is it okay to say “delete node” when it is already unallocated? I know it’s okay if you say “delete” to a NULL pointer, but what about a pointer that is pointing to unallocated memory?
Mar 8 '07 #1
7 1518
lqdeffx
39
pretty much, you answered your own question. You have 2 binary trees pointing at the same thing. so like an algebraic expression, what you do to one side you must do to the other. otherwise, when the unmodified binary tree goes to a pointer that is no longer valid, undefined behavior is the result. undefined behavior quite literally meaning formating a computer hard drive to quietly aborting the program. if you wonder why this is, i believe its because once you have "deleted" the allocated memory, you are telling the OS it is ok to use this memory space for anything it chooses.
Mar 8 '07 #2
iLL
63
pretty much, you answered your own question. You have 2 binary trees pointing at the same thing. so like an algebraic expression, what you do to one side you must do to the other. otherwise, when the unmodified binary tree goes to a pointer that is no longer valid, undefined behavior is the result. undefined behavior quite literally meaning formating a computer hard drive to quietly aborting the program. if you wonder why this is, i believe its because once you have "deleted" the allocated memory, you are telling the OS it is ok to use this memory space for anything it chooses.
However, when I go to traverse the tree who has a node that is pointing to unallocated momory, I’ll get a run time error when it tries to read from the data it is supposed to be there.

I need to set the other pointer to NULL, and the only way I can do that is by telling my template to remove a node, and my template states:

delete node;
node = NULL;

so.... I'll hit that delete before i hit node = NULL. And is that OK?
Mar 8 '07 #3
iLL
63
WOW! NO!!!!! I have to completely rethink this. Since that data doesn’t exist anymore, I’ll get a run time error when I go to remove it for the second time. When looking for that node to remove, it’ll try and read that unallocated memory.

Well crap. Never mind then!
Mar 8 '07 #4
AdrianH
1,251 Expert 1GB
WOW! NO!!!!! I have to completely rethink this. Since that data doesn’t exist anymore, I’ll get a run time error when I go to remove it for the second time. When looking for that node to remove, it’ll try and read that unallocated memory.

Well crap. Never mind then!
So you realise that you cannot delete an object that has already been deleted. Yeah that would be bad.

Use reference counting to allow for the object to be deleted multiple times until the number of references go to zero at what point it will be deleted (see boost::shared_ptr). NOTE that you cannot use this if you have an object that uses shared_ptr to zero or more chain of other objects that uses a shared_ptr back to the first (an object ownership cycle). But for what you are doing, it should be fine.

Hope this helps.


Adrian
Mar 8 '07 #5
lqdeffx
39
well dereferencing a null pointer is the least of your troubles. my experience with binary tree's is limited, but if I were in your situation... I would think of a binary tree as a triangle and would "rotate" the triangle. Thus making node->right the top dog and node->left to node->right. then create a function that would restructure my binary tree. Of course there is always google, and I'm sure other people have been in the same situation.
Mar 8 '07 #6
iLL
63
So you realise that you cannot delete an object that has already been deleted. Yeah that would be bad.

Use reference counting to allow for the object to be deleted multiple times until the number of references go to zero at what point it will be deleted (see boost::shared_ptr). NOTE that you cannot use this if you have an object that uses shared_ptr to zero or more chain of other objects that uses a shared_ptr back to the first (an object ownership cycle). But for what you are doing, it should be fine.

Hope this helps.


Adrian
Let me see if I have this straight.

I’ll make a node like:
Expand|Select|Wrap|Line Numbers
  1. struct node
  2. {
  3.     node(element):left(NULL),right(NULL),Object(element),ref(1);
  4.  
  5.     node * left;
  6.     node * right;
  7.  
  8.     Object item;    //Object is an arbitrary class
  9.     int ref;         //ref = number of references to this node.
  10. }
  11.  
And every time a pointer points to this node, then add one to ref.

Then if I go to delete the node, just set the pointer to NULL and ref--; unless reff = 1, then say delete.

Smart, Smart, Smart!!!! That’s good stuff
Mar 8 '07 #7
iLL
63
well dereferencing a null pointer is the least of your troubles. my experience with binary tree's is limited, but if I were in your situation... I would think of a binary tree as a triangle and would "rotate" the triangle. Thus making node->right the top dog and node->left to node->right. then create a function that would restructure my binary tree. Of course there is always google, and I'm sure other people have been in the same situation.
Yes! I have that figure out already. But yes, it took a little time to figure out how to keep the order of the tree while removing an object.

Thankfully, however, I don’t have to keep the tree balanced for this assignment, so I don’t have to do any rotating.
Mar 8 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Craig Stadler | last post by:
mysql 4.0.22 (win32) Can anyone recommend best practices for the fastest way to remove large numbers of rows at once? I am diving my deletes into chunks (1000 rows at a time) but this still is...
1
by: stephen atkins | last post by:
Hello all. We are just getting started with replication and I'm wondering if there is a way to not have deletes replicated. I know I could manually remove the delete trigger from every table but...
1
by: peter | last post by:
Hi All, a quick question. I haven't used DB2 on mainframe since 1995 so my memory is fading a bit. But I seem to recall DB2/MVS having the ability to perform unlogged deletes. Situation is I...
9
by: (Pete Cresswell) | last post by:
Seems like when there's a 1:1 relationship, the order of referential integrity enforcement depends on which way you drag the mouse pointer when drawing the relationship line. If you drag from...
0
by: Frnak McKenney | last post by:
One part of a customer project I'm working on involves what seem like fairly straightforward updates to a set of related tables. While I've developed software for a number of years (it only seems...
4
by: MDReed | last post by:
I am using the .config as a configuration file for my Win Forms app. "AppName.exe.config" Can any one tell me why the design environment deletes it from the bin folder? I have to copy it in and...
0
by: Raj | last post by:
We have a batch process that deletes about 1 million records every day &takes about 30-35 mins to complete. Is there a way to optimize deletes? Below is the statement delete from fact where...
6
by: Raj | last post by:
We have a batch process that deletes about 7-8 million records every day & takes about 30-35 mins to complete. Is there a way to optimize deletes? Below is the statement delete from fact where...
2
by: hikums | last post by:
Hi, Can someone recommend a better approach, I already have clustered indexes on the t1 column in table td. This takes more than 30 hours to complete!! The slowdown is with deletes within the...
0
by: Benzine | last post by:
I recently rolled out replication on our production server (MS SQL 2000 SP4) and every time a subscriber tries to sync the following always appears in the Merge Agent History: "Downloaded 100...
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?
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
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,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.