分割故障:GDB

Segmentation Fault: gdb

本文关键字:GDB 故障 分割      更新时间:2023-10-16

我是GDB的新手,一般来说,我不确定没有GDB,我不确定如何做到这一点。GDB告诉我的程序调试的程序是,此功能中存在分段故障。我将在下面的评论中强调它:

template <class elemType>
struct nodeType
{
    elemType info;
    nodeType<elemType> *lLink;
    nodeType<elemType> *rLink;
};
#define H_binarySearchTree
#include <iostream>
#include "binaryTree.h"
using namespace std;
template <class elemType>
class bSearchTreeType: public binaryTreeType<elemType>
{
public:
    bool search(const elemType& searchItem) const;
    //Function to determine if searchItem is in the binary
    //search tree.
    //Postcondition: Returns true if searchItem is found in
    //               the binary search tree; otherwise,
    //               returns false.
    void insert(const elemType& insertItem);
    //Function to insert insertItem in the binary search tree.
    //Postcondition: If there is no node in the binary search
    //               tree that has the same info as
    //               insertItem, a node with the info
    //               insertItem is created and inserted in the
    //               binary search tree.
    void deleteNode(const elemType& deleteItem);
    //Function to delete deleteItem from the binary search tree
    //Postcondition: If a node with the same info as deleteItem
    //               is found, it is deleted from the binary
    //               search tree.
    //               If the binary tree is empty or deleteItem
    //               is not in the binary tree, an appropriate
    //               message is printed.
    void printTree();
    void printTree(nodeType<elemType> *p);
    // void printTreeNode(nodeType<elemType> *p);
    void swapSubtrees(nodeType<elemType> *root1);
    void swapSubtrees();
    nodeType<elemType> *root1;
    template <class elemType>
    void bSearchTreeType<elemType>::insert
    (const elemType& insertItem)
    {
        nodeType<elemType> *current; //pointer to traverse the tree
        nodeType<elemType> *trailCurrent; //pointer behind current
        nodeType<elemType> *newNode;  //pointer to create the node
        newNode = new nodeType<elemType>;
        newNode->info = insertItem;
        newNode->lLink = NULL;
        newNode->rLink = NULL;
        if (root1 == NULL)
            root1 = newNode;
        else
        {
            current = root1;
            while (current != NULL)
            {
                trailCurrent = current;
                if (current->info == insertItem)//** This is where gdb says there is a seg fault
                {
                    cout << "The item to be inserted is already ";
                    cout << "in the tree -- duplicates are not "
                    << "allowed." << endl;
                    return;
                }
                else if (current->info > insertItem)
                    current = current->lLink;
                else
                    current = current->rLink;
            }//end while
            if (trailCurrent->info > insertItem)
                trailCurrent->lLink = newNode;
            else
                trailCurrent->rLink = newNode;
        }
    }//end insert
    template<class elemType>
    void bSearchTreeType<elemType>::swapSubtrees(nodeType<elemType> * root1)
    {
        if (root1 != NULL)
        {
            nodeType<elemType> *temp;
            swapSubtrees(root1->lLink);//Seg Fault here as well
            swapSubtrees(root1->rLink);
            temp =root1->lLink;
            root1->lLink = root1->rLink;
            root1->rLink = temp;
        }
    }
}

有人可以帮助描述正在发生的事情,以及我需要做的修复?

*编辑以包括更多详细信息非常感谢

您忘了将root1设置为构造函数中的NULL - 实际上,您似乎没有一个。