首页 > 解决方案 > 这里出现“sigbrt”错误的原因是什么?

问题描述

问题:

“连接同一级别的节点给定一棵二叉树,连接同一级别的节点。您将获得相同的附加 nextRight 指针。

最初,所有 nextRight 指针都指向垃圾值。您的函数应该将这些指针设置为每个节点的右下角。”

我的策略:

  1. 执行级别顺序遍历
  2. 将所有元素存储在向量中
  3. 表示级别的变化将 nullptr 添加到向量
  4. 迭代向量并使当前节点 nextRight 指针字段包含第 i+1 个元素的值

我的输出:
运行时错误:
来自 abort(3) (SIGABRT) 的运行时 ErrorAbort 信号

我的代码(仅限函数):

/*struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
    struct Node* nextRight;
}; */

void connect(Node *root)
{
   // vector of all node addresses
   // for each set nxtrt next ele
   // in level order add a nullptr after each level as delimiter
   queue<Node*>q;
   if(root)
   {
       q.push(root);
       q.push(nullptr);
   }
   vector<Node*>v;
   Node *curr;
   while(!q.empty())
   {
       curr = q.front();
       q.pop();
       v.push_back(curr);
       if(curr == nullptr)
       {
           q.push(curr);
           continue;
       }
       if(curr->left)
       {
           q.push(curr->left);
       }
       if(curr->right)
       {
           q.push(curr->right);
       }
   }
   
   for(int i = 0; i < (int)(v.size() - 1); ++i)
   {
       v[i]->nextRight = v[i+1]; //making each node point to right node
   }
}

我试图找出这个错误来自哪里,但无法找到它。
这种输出的原因是什么?

标签: c++pointersbinary-treetree-traversal

解决方案


在实际使用connect-g 的地方构建一些可执行文件,例如。$ g++ node.cpp -g,然后使用gdb:运行它$ gdb a.out。一旦你得到一个异常,输入bt fullhttps://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html)。这将准确地告诉你错误在哪里。试图准备一些最小的例子(虽然它不是你的用例,我相信):

#include <queue>

using namespace std;

struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
    struct Node* nextRight;
};

void connect(Node *root)
{
   // vector of all node addresses
   // for each set nxtrt next ele
   // in level order add a nullptr after each level as delimiter
   queue<Node*>q;
   if(root)
   {
       q.push(root);
       q.push(nullptr);
   }
   vector<Node*>v;
   Node *curr;
   while(!q.empty())
   {
       curr = q.front();
       q.pop();
       v.push_back(curr);
       if(curr == nullptr)
       {
           q.push(curr);
           continue;
       }
       if(curr->left)
       {
           q.push(curr->left);
       }
       if(curr->right)
       {
           q.push(curr->right);
       }
   }
   
   for(int i = 0; i < (int)(v.size() - 1); ++i)
   {
       v[i]->nextRight = v[i+1]; //making each node point to right node
   }
}

int main() {
Node t;
t.data = 5;
connect(&t);
return 0;
}

我得到的错误:

Reading symbols from a.out...done.
(gdb) r
Starting program: /home/mpiotrowski/KeyTestSpyro/PiecewiseQuadratic/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
36         if(curr->left)
(gdb) bt
#0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
#1  0x0000555555554c79 in main () at test2.cpp:55
(gdb) bt full
#0  0x0000555555554b17 in connect (root=0x7fffffffdd50) at test2.cpp:36
        q = std::queue wrapping: std::deque with 2 elements = {0x20ce2d8d48550020, 0x0}
        v = std::vector of length 5, capacity 8 = {0x7fffffffdd50, 0x0, 0x555555556c70 <__libc_csu_init>, 0x0, 0x20ce258d4c544155}
        curr = 0x20ce258d4c544155
#1  0x0000555555554c79 in main () at test2.cpp:55
        t = {data = 5, left = 0x0, right = 0x555555556c70 <__libc_csu_init>, nextRight = 0x555555554910 <_start>}

因此,基于36 if(curr->left)我们可以判断它curr是 NULL 并且您正在尝试在某些情况下取消引用它。

重要的只是一个提示:如果您对调试器感到不舒服,您可以随时添加print每一行并查看您缺少哪个打印。您将能够识别可能出现错误的行。


推荐阅读