简体   繁体   中英

C - Why am I getting a segmentation fault?

I'm a bit confused on segmentation faults apparently since I don't understand why I'm getting one. I'm trying to traverse a tree that I have built which I have tested and works fine. But, when I try to run the following function I get a segmentation fault. Any can tell me where and why I'm getting a segmentation fault so that I can avoid that in the future? Thanks.

void traverse(Node *root)
{
    Node *pointer;
    Node *pre;

    if(root == NULL)
        return;

    pointer = pre;

    while(pointer != NULL)
    {
        if(pointer->leftChild != NULL)
            pointer = pointer->rightChild;
        else
        {
            pre = pointer->leftChild;

            while(pre->rightChild != NULL && pre->rightChild != pointer)
                pre = pre->rightChild;

            if(pre->rightChild != NULL)
            {
                pre->rightChild = pointer;
                pointer = pointer->leftChild;
            }
            else
            {
                pre->rightChild = pointer;
                pointer = pointer->leftChild;
            }
        } 
    }
}

The pointers initially contain the garbage value as you are not initialing it in your code.

Node *pointer;
Node *pre;

Since they contain garbage value, they may not point to NULL and where they would be pointing would be allocated to some other resources, which cannot be accessed causing segmentation fault in following line.

if(pointer->leftChild != NULL)
        pointer = pointer->rightChild;

You need to either initialize your pointers or assign to it input argument.

Edit

One more problem i could observe in your code is at if-else condition. In your if condition, you are checking if(pointer->leftChild != NULL) so if this condition is false (ie pointer->leftChild = NULL ), you will fall into else block, where you are assigning like this;

pre = pointer->leftChild; // Here pre is always NULL

So if you will try to use it further, it will throw exception hence makes your code to crash.

You have to initialize Node *pre = NULL; and Node *pointer = root; . You did not initialize pre but assigned pointer = pre; .

Apart form this you should change this:

if ( pointer->rightChild!= NULL)
           // ^^^^^^^^^^ change leftChild to rightChild 
    pointer = pointer->rightChild;

If the nodes of a tree have no pointer to the parent node, it is typically used a recursive function to traverse through the tree:

void traverse( Node *root )
{
    if ( root == NULL )
        return;

    traverse( root->leftChild );

    // do somethig with root

    traverse( root->rightChild );
}

Note the parameter of the recursive function takes the place of the stack which is necessary to go back from a leaf node to the root node.

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