0

I'm having a little trouble with a critical function in my program, I've put bug testing lines everywhere and singled it down to a single if statement"

template <typename Item>
bool BTNode<Item>::isNull(string leftOrRight)
{
    std::cout<<"Is NULL test outer."<<endl;
    bool returnNullTest = true;
    if (leftOrRight == "left")
    {
        std::cout<<"Is NULL test inner 1."<<endl;
        if (left != NULL)
        {
            returnNullTest = false;
        }
    }
    else if (leftOrRight == "right") //.c_str()
    {
        std::cout<<"Is NULL test inner 2."<<endl;
        if (right != NULL)
        {
            returnNullTest = false;
        }
    }
    std::cout<<"NULL TEST FINISHED."<<endl;
    return returnNullTest;
}

this is the output:

Is NULL test outer.
Is NULL test inner 2.
Segmentation fault (core dumped)

this is the definition of 'left' and 'right':

BTNode<Item>* left;
BTNode<Item>* right;

in the constructors for BTNode 'left' and 'right' are defined as:

left = NULL;
right = NULL;

does anyone have an idea as to where i'm going wrong with this, i've tried the line

if (left == NULL)

and

if (right == NULL)

with the Boolean switched around but I got the same error.

this is 'BTNode.h'

#ifndef matt_BTNode
#define matt_BTNode
#include <cstdlib>
namespace mattassign3{
template <typename Item>
class BTNode
{
    private:
    Item* data;
    BTNode<Item>* left;
    BTNode<Item>* right;

    public:
    BTNode();
    BTNode(Item* startingData);
    ~BTNode();
    BTNode<Item>* getLeft();
    BTNode<Item>* getRight();
    Item* getData();
    bool isNull(string leftOrRight);
    void setLeft(BTNode<Item>* leftToSet);
    void setRight(BTNode<Item>* rightToSet);
    void printInclData();
    float comparableNumber();
    string comparableString();
};
}
#include "BTNode.template"
#endif
21
  • 2
    What is the type of BTNode? Commented Nov 8, 2013 at 8:38
  • 2
    My spidey sense tells me the crash occurs after this function returns; you just don't get to see the output of the last cout statement printed for reasons which are not important to this discussion. Now would be a good time to learn how to use a debugger: google for gdb. Commented Nov 8, 2013 at 8:39
  • 1
    well, if you run this as a standalone function theres no problem, it must be somewhere else in your code Commented Nov 8, 2013 at 8:52
  • 1
    @MatthewMuller If the problem cannot be reproduced in every call to isNull, you clearly haven't isolated it correctly. I have provided my diagnostic, now it is your turn to debug your code. Commented Nov 8, 2013 at 8:52
  • 1
    @MatthewMuller This is not a discussion forum. I have no need to convince you that you have a bug elsewhere in your code. I'm just saying it. You are free to not believe it and keep looking at the body of isNull for a few more hours. Commented Nov 8, 2013 at 8:54

2 Answers 2

3

Most likely this is an invalid pointer. The function crashed the first time a member of this was accessed. Try printing this along with the first message.

Sign up to request clarification or add additional context in comments.

2 Comments

@kfsone Could be anything, including calling a member function on a left or right link which was set to null.
i've tested the BTNode that goes through and this is right, i've managed to break it along the way. is there any common mistakes that could cause this?
3

The fact that you crash when accessing "right" is a symptom of a bug higher in your call stack. Specifically, prior to arriving at this point in your program you must have done something like, e.g.

BNode* node = something->getRight();
node->isNull();

You need to remind yourself that in your isNull function right and left are member variables. Their locations in memory are relative to the instance of BNode they belong to, they are not just some local variables (this is another good reason why many programmers choose to distinguish member variables with a prefix like "m_", e.g. "m_left", "m_right").

If the "BNode*" pointer you are calling "isNull" against is bad, then it helps to remember that when you say

if(left != NULL)

you are accessing *(this + 4 bytes). You're "getting away" with access "left", but "right" is unfortunately on the other side of some kind of memory boundary that results in your crash.

Make sure: Your constructors assign default values to these pointers, that you check return values from getLeft() and getRight() before derefencing them, that your copy constructor does NOT copy these values (that would mean there are two Nodes that think they are at the same position in the tree) and that your destructor either asserts if the node is not unlinked or unlinks the node.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.