c++ - 未知的段错误?

标签 c++

我的代码有问题。我已经运行调试了好几次了。如果我不在我的 getEntry 函数中抛出异常,它似乎工作正常。但是当我确实抛出异常时,我的程序在那之后出现了段错误。当我通过程序调试时,getEntryHelper 中的 nextNodePtr 似乎不是 0x0。所以它在抛出异常后以某种方式发生了变化,我不知道为什么。

我的主要内容:

#include <iostream>
#include "BinarySearchTree.h"

int main {
BinarySearchTree<std::string,std::string> myTree;
    myTree.add("book");
        myTree.add("encyclopedia");
    myTree.add("automobile");
    myTree.add("zebra");
        myTree.getEntry(zebra);
        myTree.getEntry(xylophone);
        myTree.getEntry(tree); // Does not get to here

}

这是我的 add an 和 getEntry 方法(尽管看起来我的 getEntry 是问题所在:

template<typename KeyType, typename ItemType>
void BinarySearchTree<KeyType,ItemType>::add(const ItemType& newEntry) {
    if(rootPtr == NULL)
        rootPtr = new BinaryNode<ItemType>(newEntry);
    else {
        addHelper(rootPtr,rootPtr,newEntry);
    }
}

template<typename KeyType, typename ItemType>
ItemType BinarySearchTree<KeyType,ItemType>::getEntry(const KeyType& aKey) const
        throw(NotFoundException) {
    try {
        BinaryNode<ItemType>* temp = getEntryHelper(rootPtr,aKey);
        std::cout << temp->getItem() << "\n";
        return temp->getItem();
    }
    catch(NotFoundException& nf) {
        std::cout << nf.what();
    }
}

template<typename KeyType, typename ItemType>
void BinarySearchTree<KeyType,ItemType>::addHelper(BinaryNode<ItemType>* prevNodePtr,
                                                    BinaryNode<ItemType>* nextNodePtr,
                                                    const ItemType& newEntry) {

    if(nextNodePtr == NULL) { // Base Case
        nextNodePtr = new BinaryNode<ItemType>(newEntry,NULL,NULL);
        if(newEntry < prevNodePtr->getItem())
            prevNodePtr->setLeftChildPtr(nextNodePtr);
        else
            prevNodePtr->setRightChildPtr(nextNodePtr);
        return;
    }
    if(newEntry < nextNodePtr->getItem()) {
        prevNodePtr = nextNodePtr;
        nextNodePtr = nextNodePtr->getLeftChildPtr();
        addHelper(prevNodePtr,nextNodePtr,newEntry);
    }
    else {
        prevNodePtr = nextNodePtr;
        nextNodePtr = nextNodePtr->getRightChildPtr();
        addHelper(prevNodePtr,nextNodePtr,newEntry);
    }
}

template<typename KeyType, typename ItemType>
BinaryNode<ItemType>* BinarySearchTree<KeyType,ItemType>::getEntryHelper(BinaryNode<ItemType>* nextNodePtr,const KeyType& aKey) const {
    if(nextNodePtr == NULL) {
        throw NotFoundException("does not exist in tree.\n");
    }
    else if(nextNodePtr->getItem() == aKey)
        return nextNodePtr;
    else if(aKey < nextNodePtr->getItem()) {
        getEntryHelper(nextNodePtr->getLeftChildPtr(),aKey);
    }
    else {
        getEntryHelper(nextNodePtr->getRightChildPtr(),aKey);
    }
}

输出: 汽车 书 百科全书 斑马 斑马 违反先决条件异常:树中不存在木琴。 段错误(核心转储)

最佳答案

快速浏览一下,我发现几乎没有异常处理不当的问题。

  1. 函数 getEntry() 表示它可以抛出 NotFoundException,但在 main() 中我看不到它的任何异常处理程序。所以在 main() 函数中放置一个基本的 try catch,它可以处理任何异常。

    int main()
    {
     try
     {
      //some code
     }
     catch(..)
     {
      cout << "Unkown Exception";
     }
     return 0;
    }
    
  2. 函数 getEntry() 表示它可以抛出 NotFoundException,但您有一个 try catch block ,您可以在其中处理异常但绝不会重新抛出,也不会抛出任何新\修改的 NotFoundException。如果您不想抛出它,则在函数声明中注释掉 throw(NotFoundException)。

        ItemType BinarySearchTree<KeyType,ItemType>::getEntry(const KeyType& aKey) //const throw (NotFoundException) -> Comment this
    

如果在处理 NotFoundException 后没有,则重新抛出。

catch(NotFoundException& nf) {
    std::cout << nf.what();
    rethrow;
}

但我仍然不确定您的二进制代码插入逻辑是否工作正常。如果您遇到任何逻辑问题,请发布整个 header 和 cpp 文件。

关于c++ - 未知的段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16284102/

相关文章:

c++ - 这种类型的双关语合法安全吗?

c++ - 未分配某些元素的数组中存储了什么值?

c++ - 如何在 C++ 中动态分配指针数组?

c++ - 使用 c while 循环循环所有整数数组元素

c++ - fetch_sub 真的是原子的吗?

c++ - 取消引用运算符给出的结果与带有 void* 的数组偏移运算符不同

c++ - 部分模板特化可能不适用于函数,但重载不是同一件事吗?

c++ - LabVIEW Linux C++开发——初学者问题

c++ - 将 C++ 共享库链接到 C 程序时如何避免错误

c++ - 推力异常 : "thrust::system::system_error at memory location 0x00000000"