A related question is, when should you use new to create objects, and what is the difference? I was able to stop 20 out of the 100 bytes I had leaking previously simply by removing the new operator when I was creating objects. Furthermore, this seemed to have no effect on my test cases at all, which is curious, because I thought when you don't use new to create objects, they go out of scope when the method returns.
Also, here is what I am doing to insert nodes into the BST:
BSTNode<Data>* nodeInsert(BSTNode<Data>* node, const Data& item) const
{
if(item == node->data)
return 0;
else if(item > node->data)
{
if(node->right != 0)
return nodeInsert(node->right, item);
else
{
BSTNode<Data>* newNode = new BSTNode<Data>(item);
node->right = newNode;
newNode->parent = node;
return newNode;
}
}
else if(item < node->data)
{
if(node->left != 0)
return nodeInsert(node->left, item);
else
{
BSTNode<Data>* newNode = new BSTNode<Data>(item);
node->left = newNode;
newNode->parent = node;
return newNode;
}
}
}
And here is what I am doing in my destructor which should free all of the nodes I created:
void deleteNode(BSTNode<Data>* node)
{
if(node->left != 0)
{
deleteNode(node->left);
node->left = 0;
delete node->left;
}
else if(node->right != 0)
{
deleteNode(node->right);
node->right = 0;
delete node->right;
}
else
{
node->parent = 0;
delete node;
}
}
Both of those are helper functions that are called from another function, if that makes a difference.
This post has been edited by ModusPwnens: 08 October 2010 - 11:31 AM

New Topic/Question
Reply




MultiQuote






|