Operator overloading produces bad pointers

Hello all. I'm working on creating a binary search tree that is stored in an array for my CS class. However, I've hit a hiccup that I just cannot get past no matter what I try.

What I have is two classes. One class is called data and it has a cString called name as it's only datamember. In this class I have a constructor, copy constructor and operator overload. They look like this:

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
Data::Data(char const * const name) :
	name(new char[strlen(name) + 1])
{
	//Copy the name parameter to the name variable
	setName(name);
}

// copy constructor
Data::Data(const Data& source) : name(new char[strlen(source.name) + 1])
{
	//Copying the name from the source data item to the name variable.
	strcpy(name, source.name);
}

// assignment operator overload
Data& Data::operator=(const Data& data2)
{
	delete[] name;

	name = new char[strlen(data2.name) + 1];

	//Copying the parameter chars into the return objects data.
	strcpy(name, data2.name);

	return *this;
}


setName looks like this:

1
2
3
4
5
6
7
8
9
void Data::setName(char const * const name)
{
	delete[] this->name;

	this->name = new char[strlen(name) + 1];

	//Copy the name parameter to the name variable
	strcpy(this->name, name);
}


I then have another class called BST. It has a pointer of type Item (which is a struct I made) which is called items. This holds the array.

The private section of the BST class looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private:
	// specify what is stored at each position in the array
	struct Item
	{
		Item();				//Constructor.
		Data	name;		//Holds the name of the comp scientist
		int		nodeIndex;	//Array position of the node
		int		rightIndex;	//Array position of the nodes right child
		int		leftIndex;	//Array position of the nodes left child
		bool	leaf;		//Used to determine if the node is a leaf
		bool	full;		//Used to determine if the node contains data.
	};

	// pointer to the array
	Item	*items;
	int		size;			//Number of nodes in BST
	int		capacity;		//Capacity of the array. 


My insert function looks like this:

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
// insert a new item into the BST
void BST::insert(const Data& data)
{
	int	 currIndex = 0;		//The root is at index 0
	bool loopFlag = false;	//When true, the loop should stop

	do
	{
		cout << currIndex << endl;

		if(!items[currIndex].full)
		{
			items[currIndex].name.setName(data.getName());
			items[currIndex].leaf = true;			//Always starts as a leaf
			items[currIndex].full = true;			

			items[currIndex].nodeIndex = currIndex;	//Sets the index of the node.
			size++;

			loopFlag = true;						//The loop should stop now.
		}
		else if(data < items[currIndex].name)
		{
			if(items[currIndex].leaf)
			{
				items[currIndex].leaf = false;
				items[currIndex].leftIndex = (2 * currIndex) + 1;	//The left child is at 2 * I + 1
			}

			currIndex = items[currIndex].leftIndex;	//Setting the current Index to the left child.
		}
		else
		{
			if(items[currIndex].leaf)
			{
				items[currIndex].leaf = false;
				items[currIndex].rightIndex = (2 * currIndex) + 2;	//The right child is at 2 * I + 2
			}
			
			currIndex = items[currIndex].rightIndex;
		}

		//Increases the array size if currIndex gets too big.
		if((currIndex >= capacity) || (size == capacity))
		{
			//The new array needs to be twice as big
			Item *newArray = new Item[capacity * 2];

			for(int i = 0; i < capacity; i++)
			{
				newArray[i] = items[i];
			}

			//Avoid a memory leak
			delete[] items;

			//Set the pointer to the new array and set the capacity
			items = newArray;
			newArray = NULL;	//Just to be safe
			capacity *= capacity;
		}
	} while(!loopFlag);
}


What keeps happening is the first three names that I add to the array insert correctly. The fourth name tries to add a node to an index outside of the array index, so the array needs to grow. This is where my code stops working. As I'm assigning newArray[i] = original[i], the overloaded '=' operator from data is called. Visual studio breaks inside of the overloaded operator because both name and data2.name are bad pointers.

I've spent 3 hours now working on this and I'm stumped. I have no idea why the operator = from data is being called when it isn't a friend in the BST class. Let alone as to why bad pointers are being sent. Any ideas?
Last edited on
Item *newArray = new Item[capacity * 2];

capacity *= capacity;

Notice any inconsistency there?
Oops. My mistake there. I had capacity *= 2; earlier but I tried a different technique that failed and forgot to change it back.

Even with that fix, the line name = new char[strlen(data2.name) + 1]; still says that name and data2.name are bad pointers. I'm stumped.
Your code sets leaf to false when one of the child nodes becomes occupied. Your code also assumes that each child's rightIndex and leftIndex is correct when a node's leaf variable evaluates to false. Only one of them will be correct.

I'm not sure why you have the rightIndex, leftIndex and nodeIndex variables anyway. You always know which index you're at because you're using the index to navigate, and the right and left index are easily calcuable from the index you're at.
Topic archived. No new replies allowed.