核心已转储,找不到原因

时间:2019-05-16 18:53:00

标签: c++

我写了一个代码,其功能是将值放入二叉搜索树中,然后检查左儿子和父亲是否相等。 该代码在Windows中工作正常,但我在Linux上对其进行了检查,并抛出了一个核心的Dumpted错误。

我用GDB进行了测试以查找错误,然后我转到第82行(insert_into_arr函数,也已在代码中标记)。我无法弄清错误的根源,如果您关注此问题,我将很高兴。 input data: 8 8 output: 1

#include <iostream>
#include <iomanip>
#include <cstdlib>
//------------------------------------------------
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::nothrow;
//------------------------------------------------
struct Node
{
    int _data;
    struct Node *_left,
                *_right;
};
//------------------------------------------------
const int S = 100;
//------------------------------------------------
void build_bst(struct Node* &root, int arr[S]);
void insert_into_bst(struct Node* new_node, struct Node* &root);
void insert_into_arr(const int num, int arr[S]);
void arr_check_n_print(const int arr[S]);
void free_tree(struct Node* root);
void memory_failure();

//------------------------------------------------
int main()
{
    struct Node *root = NULL; //pointer to the root of the binary search tree
    int arr[S] = {0}; //assist arr for store values

    build_bst(root,arr); //call build bst function
    arr_check_n_print(arr); //call check array and print function
    free_tree(root); //call free memory

    std::cin.get(); std::cin.get();
    return EXIT_SUCCESS;
}
//------------------------------------------------
void build_bst(struct Node* &root, int arr[S])
{           //main build tree function receives root and arr by reference
    int num;

    cin >> num; //input into num
    arr[1] = num;  //first input will represent the root of the tree
    while (num != cin.eof() && num > 0)
    {
        //main input loop runs until eof & num > 0 (exercise request)
        struct Node *new_node = new (std::nothrow) struct Node; //potential
        if (new_node == NULL)
            memory_failure(); //cannot allocate memory

        new_node->_data = num; //update data like num
        new_node->_left = new_node->_right = NULL; //update to null for ending
        insert_into_bst(new_node, root);  //insert values into the bst by sort

        cin >> num; //new input
        if(num != cin.eof())
            insert_into_arr(num,arr); //insert into assist array
    }
}
//------------------------------------------------
void insert_into_bst(struct Node* new_node, struct Node* &root)
{
    //insert into bst function receives root by reference & new input by value
    if (root == NULL)
        root = new_node; //if it's the first element
    else if (root->_data >= new_node->_data) //sort checks & assume
        insert_into_bst(new_node, root->_left);
    else
        insert_into_bst(new_node, root->_right);

}
//------------------------------------------------
void insert_into_arr(const int num, int arr[S])
{
    //insert into assist array function receives 2 arguments
    int i = 1;
    while(true) //seems like no end loop but it has return in it
    {
        if(num <= arr[i] && arr[i*2] == 0) //algorithm check ** core dumpted lin
        {
            arr[i*2] = num; //algorithm assume
            return;
        }
        else if(num > arr[i] && arr[i*2+1] == 0) //algorithm check
        {
            arr[i*2+1] = num; //algorithm assume
            return;
        }
        else if(num <= arr[i]) //algorithm check for index increase
            i++;

        else
            i+=2;
    }
}
//------------------------------------------------
void free_tree(struct Node* root) //free memory function
{
    if (root != NULL)
    {
        free_tree(root->_left);
        free_tree(root->_right);
        delete root;
    }
}
//------------------------------------------------
void arr_check_n_print(const int arr[S]) //checking the equal values n printing
{
    int counter = 0;

    for(int i=1; i < S; i++) //runs until size
    {
        if(arr[i] == arr[i*2] && arr[i] != 0) //check if equal
            counter++;
    }
    cout << counter << endl; //output data
}
//------------------------------------------------
void memory_failure()
{
    cerr << "Cannot allocate memory\n";
    exit(EXIT_FAILURE);
}

1 个答案:

答案 0 :(得分:5)

while (num != cin.eof() && num > 0)

当cin到达文件末尾时,这等效于     while(num!= 1 && num> 0) 因此,除非您的最后一个数字为1或负数,否则您将进入一个无限循环,添加最后一个数字,直到程序崩溃或Universe结束。

具体来说,cin.eof()返回布尔值,因此您正在比较int != bool。这会使布尔值转换为0(假)或1(真)。

我认为您想要的是while (!cin.eof() && num > 0)或更好的东西:

while ((cin >> num) && num > 0)