简体   繁体   中英

Weird segmentation fault errors

Ok, so im working on an assignment and for the life of me I cannot figure out why I am getting these segmentation faults. Im still in the process of learning c++, programming in general, so I was hoping someone wiser than me can help me out. The program is a self organizing binary search tree and I wasn't having too much difficulty with it until now. Here is the beginning of a main program I am using to test my class BST, I cannot alter the mian program since it is an assignment.

int main() {
string input;

// get a list of integer values
cout << "Enter a list of integer values in one line: ";
getline(cin, input);

cout << "\n**CHECKPOINT 1**\n";

// create a binary search tree
BST<int> bst1(input);


if (!bst1.empty()) {
    cout << "\n**CHECKPOINT 2**\n";
    cout << "Inorder traversal: ";
    bst1.printInOrder();
    cout << "Level order traversal: ";
bst1.printLevelOrder();

I have yet to get past the printInOrder() function, here is the code for that

template <typename T>
void BST<T>::printInOrder(BSTNode* t) const
{
    if (t->left != NULL)
        printInOrder(t->left);
    std::cout << " " << t->data << " ";
    if (t->right != NULL)
        printInOrder(t->right); 
}

The really strange thing that is confusing me if that when I add a quick cout<< "Something" to the first line of the printInOrder function, it all of a suddent will the print line

cout << "Inorder traversal: ";

and it will also start printing some of the numbers in the tree before finally giving me a segmentation fault again. :/

So, I would be really grateful if someone could explain to me WTF is going on. The adding or subtracting of a simple cout line shouldn't change things like that should it? Also, I feel like there are better ways of debugging this, if anyone has techniques that they use to figure this stuff out, please share :) Thanks in advance!

EDIT: I have tried the debugger GDB, I was unable to figure it out, but then again im not very well versed in the advanced features of debuggers so I might have missed something. The only other function that is even run, is the constructor to build from the string input. From what I could tell from the debugger is that the constructor seems to be working fine but nonetheless here's the code

template <typename T>
BST<T>::BST(const std::string input, int th)
{
    threshold = th;
    root = NULL;        
    T v;
    // Make Input String Stream for easy use of >> operator
    std::istringstream iss (input);
    do
    {
        iss >> v;
        insert(v, root);
    }while(iss.good());
}

EDIT2:

Here is the code for my insert function, Thanks for the help everybody! :)

template <typename T>
void BST<T>::insert(const T& v, BSTNode *&t)
{
    if(t == NULL)
        {
            t = new BSTNode;
            t->left = NULL;
            t->right = NULL;
            t->data = v;
            t->searchCount = 0;
        }
    else if( v < t->data )
        insert(v, t->left);
    else
        insert(v, t->right);
}

There's a marked lack of newlines in your output. Often the line buffering means you don't see anything until a newline is encountered.

I'd modify the line after the PrintOnOrder to this:-

    cout << "\nLevel order traversal: ";

In the constructor you are inserting v into the tree even if reading the data with iss >> v failed. Probably you rather want something like this:

while (iss >> v) {
   insert(v, root);
}

But probably the real cause for your segmentation fault lies in insert() , for example if that function just inserts a pointer to the (stack allocated) parameter it receives into the tree. The parameter variable will go out of scope at the end of the function (and therefore cease to exist). If you just stored a pointer to that variable into the tree, that pointer will no longer point to anything useful.

I don't see anything wrong here. As others pointed out, output buffering may mean that your code actually completes printInOrder() successfully and then crashes somewhere later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM