Ubuntu Forums Archive Viewer

my tree cant delete its children

Archived thread 1003519 from Packaging and Compiling Programs. Markdown source: Packaging_and_Compiling_Programs/thread_1003519_my_tree_cant_delete_its_children.md

Original URL About this archive
#1

I was making a tree, but when I ran it, it would not delete its children (I started out by trying to make it able to correctly delete its children) I made a small program that demonstrates this, when I run it, it does not crash, despite the extra delete of the third node

#include <iostream>

/////////////////////////////////////////////////////////

class Node {
	Node* child;
	
	public:
		Node(Node* setChild = NULL);
		~Node();
};

Node::Node(Node* setChild) {
	child = setChild;
}

Node::~Node() {
	if(child != NULL) {delete child;}
}

///////////////////////////////////////////////////////

int main() {
	Node *first, *second, *third;
	
	third = new Node();
	second= new Node(third);
	first = new Node(second);
	
	delete first;
	delete third; //this should crash it
	
	return 0;
}