• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Destroying objects in c++

Carlis

Senior member
Hi

I am developing a program in c++. Now, I have objects stored in heap space;

Graph *GR;
GR=new Graph(...)

The objects of type Graph in turn own binary trees of other objects also stored in heap, i.e..

Node *N.
N=new Node(...)

So the nodes have pointers to other nodes etc.

Now, I want to destroy the object GR and all the Nodes belonging to it. In java, one would just put =null, but that does not work in c++, right?
 
You have to write a destructor for Graph that will clean up memory allocated by Nodes. Then you call delete GR.
 
You'll have to do something like this, in the code to free GR it will be
Code:
delete GR;
GR = NULL;

in the Graph destructor it will look something like
Code:
Graph::~Graph()
{
   delete N;
   N = NULL;
}

C++ automatically calls the objects destructors when the delete command is issued. Just realize that with the graph, the deleting of the nodes is going to be more complex then what i've written, each node will have to ensure that its child nodes are deleted, there is also going to have to be some sort of signaling going on to let the other nodes know when a child node is deleted.
 
If you need a fixed number of nodes, say always 2, you can make them member variables of type Node instead of pointers to Node. Then you don't need to new or delete them separately.

class Graph {
Node n1, n2 ;
}

... this does assume you can initialize them after construction.
 
You'll have to do something like this, in the code to free GR it will be
Code:
delete GR;
GR = NULL;

in the Graph destructor it will look something like
Code:
Graph::~Graph()
{
   delete N;
   N = NULL;
}

C++ automatically calls the objects destructors when the delete command is issued. Just realize that with the graph, the deleting of the nodes is going to be more complex then what i've written, each node will have to ensure that its child nodes are deleted, there is also going to have to be some sort of signaling going on to let the other nodes know when a child node is deleted.


implementing the node deletion is simple in trees, you just do a depth first traversal and delete when you coem to something with no children... how to do it in graphs with circular pointers is really difficult.
 
implementing the node deletion is simple in trees, you just do a depth first traversal and delete when you coem to something with no children... how to do it in graphs with circular pointers is really difficult.
not really, each child node just has to inform their parents or neighbors that they are being disconnected. This can be accomplished by having the child keep a list of who points to the child node and then sending a message to each node that points to it that this node is being deleted and they need to remove it from their lists.

Another method would be to have the graph create a list of nodes and manage new node creation via the list, IE

Code:
Node* nodes = new Node[512];

That way, when the Graph is deleted, it just has to delete the array of Nodes. But that adds the management overhead of what happens with node creation. However, it would be fast as the memory would be allocated in one swoop vs several.
 
If Graph has inheritance or is inherited, it is best practice to declare destructors as virtual and make sure a proper hierarchy cleanup is done.
 
Make sure each class cleans itself up.

Here is an example:

Code:
Graph (class)
BST (class)
MyObject(class)


Graph::~Graph() {
	delete myBST1;
	myBST1 = NULL;

	delete myBST2;
	myBST2 = NULL;
}

------------------------

BST::~BST() {
	clear();
}

BST::clear() {
	remove(rootNode);
	rootNode = NULL;
	size = 0;
}

BST::remove(BSTNode * &node) {
 	if (node != NULL) {
            remove(node->left);
            remove(node->right);
            delete node;
            node = NULL;
        }

}

-------------------------

MyObject::~MyObject() {
	//add clean up code
}
 
Last edited:
Back
Top