gpt4 book ai didi

c++ - 设置 bool 时可能出现段错误?

转载 作者:行者123 更新时间:2023-11-27 22:48:25 26 4
gpt4 key购买 nike

我有一个二叉树,其节点定义为

typedef unsigned long ul;
struct Fibonacci_node{
ul number;
int n;
bool isLeaf;
Fibonacci_node * left;
Fibonacci_node * right;
};

我想在每次插入时都设置isLeaf,这样我就可以很容易地得到叶子的总数。插入方法由调用私有(private)递归方法 insertR 的公共(public)方法 insert 组成。

#include <iostream>
using namespace std;

class Fibonacci_tree{
private:
struct Fibonacci_node{
ul number; // store the n-th fibonacci number
int n; // fibonacci number to compute
bool isLeaf; // true if the node is leaf
Fibonacci_node * left;
Fibonacci_node * right;
};



/* definition of the root of the binary tree */
Fibonacci_node * root;

/* class private methods (recursively defined) */
ul fibonacci(int n){
/* BASE CASE */
if (n == 0) { return 1; }
if (n == 1) { return 1; }

/* call the function recursively */
return fibonacci(n - 1) + fibonacci(n - 2);
};

Fibonacci_node * insertR(int n, Fibonacci_node * node){
if (!node) {
/* if pointer is null create a new node */

Fibonacci_node * tmp = new Fibonacci_node;
tmp->n = n;
tmp->number = fibonacci(tmp->n);
tmp->left = tmp->right = 0;
tmp->isLeaf = 0;

/* update the pointer and return it */
node = tmp;

/* BASE CASE */
if (n == 0) {
node->isLeaf = 1;
return node;
}
if (n == 1) {
node->isLeaf = 1;
return node;
}
}

/* call the function recursively */
node->left = insertR(n - 1, node->left);
node->right = insertR(n - 2, node->right);

return node;
};

public:
Fibonacci_tree(){
root = 0;
}
~Fibonacci_tree(){}

/* class public methods (they include private methods recursively defined)*/
void insert(int n){

/* first, create initial node and compute fibonacci for the root */
Fibonacci_node * tmp = new Fibonacci_node;
tmp->n = n;
tmp->number = fibonacci(n);
tmp->isLeaf = false;
//getNo(tmp);

/* make root point to the first element of the tree */
root = tmp;

/* then call the recursive function */
root = insertR(n, root);
};
};
/* END OF CLASS DECLARATION */


/* main program to check the class */
int main(void) {
int n = 3;

/* instantiate a Fibonacci tree */
Fibonacci_tree fib_series;
/* fill the tree */
fib_series.insert(n);

return 0;
}

当我运行我的可执行文件时,我得到

Segmentation fault: 11

我打印了几条评论,我注意到错误似乎是在我将“false”分配给 boolean 值之前立即出现的。所以,我假设分配有误,但这是我第一次在这种情况下遇到段错误。

我也认为,因为到目前为止我没有遇到任何问题,但是当我在类定义中引入这个变量时它就开始了。

是否可能因此而出现段错误,或者我可能在其他地方遇到了我尚未注意到的问题?

我想完全自己调试,但是我的调试技术还是有点烂。因此,非常感谢任何反馈。

最佳答案

实际问题在这里:

 /* call the function recursively */
node->left = insertR(n - 1, node->left);

此时 node->left 尚未初始化,您将那个未初始化的值递归地传递给 insertR,在那里您检查它是否为 NULL,因为它是非初始化的,它很可能是非空的,然后你在这里再次取消引用它:node->left = insertR(n - 1, node->left);。取消引用未初始化的指针是未定义的行为。

实际上,您只是忘记了将 leftright 初始化为 0:

/* first, create initial node and compute fibonacci for the root */
Fibonacci_node * tmp = new Fibonacci_node;
tmp->n = n;
tmp->number = fibonacci(n);
tmp->isLeaf = false;
tmp->left = tmp->right = 0; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< you forgot this.
//getNo(tmp);

当您使用 C++ 编写时,为什么不为 Fibonacci_node 编写一个构造函数,所有初始化都可以在一个地方完成?

tmp->isLeaf = false; 在您的计算机上导致段错误只是未定义行为的结果。

关于c++ - 设置 bool 时可能出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40615186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com