Debug assertion failed: _BLOCK_TYPE_IS_VALID(phead->nBlockUse)

Hey,

So I'm trying my hands with pointers by doing a binary search tree, however I've hit an other roadblock again. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class node
{
	public:
	node* left;
	node* right;
	node* root_;
	int value;

	node(int num, node* root)
	{
		value = num;
		left = right = NULL;
		root_ = root;
	}

	~node()
	{
		delete left;
		delete right;
		delete root_;
	}

	node* find(int num, node& root)
	{
		node* FoundNode;
		if(root.value < num)
		{
			if (root.right == NULL)
			{
				FoundNode = &root;
			}
			else
			{
				FoundNode = find(num, *root.right);
			}
		}
		if (root.value > num)
		{
			if (root.left == NULL)
			{
				FoundNode = &root;
			}
			else
			{
				FoundNode = find(num, *root.left);
			}
		}
		else
		{
			FoundNode = &root;
		}

		return FoundNode;
	} 
	//returns pointer to element if found or pointer to last element searched.

	void insert(int num, node& root)
	{
		node* position = find(num, root);
		node pos = *position;
		if (pos.value < num)
		{
			node temp(num, position);
			pos.right = &temp;
		}
		else if (pos.value > num)
		{
			node temp(num, position);
			pos.left = &temp;
		}
	}
	//inserts element in correct position if not found.
void traverse(node root)
	{
		if (root.left != NULL)
			traverse (*root.left);
		if (root.right != NULL)
			traverse (*root.right);
		cout << root.value;
	}
}


1
2
3
4
5
6
7
8
int main()
{
	node root (10, NULL);
	root.insert(9, root);
	root.traverse(root);
	system("sleep");
	return 0;
}


I'm completely new to pointers. I've searched the net a little and found out that it has to do with wrongly allocated memory of some sort. While trying to debug this thing, I noticed it crashes as soon as the function "insert" finishes. What am I doing wrong?
Last edited on
In your destructor, you're trying to delete something you shouldn't be trying to delete. Replace your destructor with this and see if you can work it out :)

1
2
3
4
5
6
7
cout << left << " " << right << " " << root << endl;
cout << "Now delete left" << endl;
delete left;
cout << "Now delete right" << endl;
delete right;
cout << "Now delete root_" << endl;
		delete root_;


There are many problems with this code.

You are not using any of the member variables of the object that the function is called on which is a bit weird. You should not have to pass in the root node as argument to the functions. The node itself is the root of the sub tree.

When you create a node like you do on line 60, 63 and 68 the node will stop to exist when it goes out of scope. If you want to create a node object that stays alive after the current scope has ended you can use new. node temp(num, position); becomes node* temp = new node(num, position);
You have to keep at least one pointer to the object otherwise you will not be able to reach it later.

On line 37 you should have else if.
Last edited on
Well I removed the delete root_; command, and it seems to run without errors. But, why is it calling the destructor so many times? I don't understand. I thought it only calls the destructor when something isn't "reachable" anymore.

In addition, my insert method isn't working. I'm passing the values by reference, but it doesn't seem to change the "root" object in the main feature. Why is that?
Peter, you mean like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void insert(int num)
	{
		node* position = find(num, *this);
		node pos = *position;
		if (pos.value < num)
		{
			pos.right = new node(num, position);
		}
		else if (pos.value > num)
		{
			pos.left = new node(num, position);
		}
	}
	//inserts element in correct position if not found. 


It still seems to delete the element.
Last edited on
pos will be deleted at the end of the insert function. If I understand it correctly you want to modify the node object pointed to by position. In that case just use position->right and position->left and remove pos from your code.
Oh thank you so much. It works now! I'll ask more questions here if I experience any road blocks.

Regards
Everythings working now, and I've written my first datastructure, the binary tree, with basic functions like insert, remove, post-order traverse and height calculation. Thanks again for all the help I got.
Topic archived. No new replies allowed.