【问题标题】:Why is my code showing error Segmentation fault(core dumped)为什么我的代码显示错误分段错误(核心转储)
【发布时间】:2016-07-11 22:33:02
【问题描述】:

我试图在二叉树中实现前序遍历。这是我的代码 sn-p。

#include <iostream>
using namespace std;

struct node{
   int data;
   struct node *left;
   struct node *right;
};

struct node *root= NULL;

struct node *inorder_search(struct node *node, int val){
   inorder_search(node->left,val);
   if(node->data==val)   return node;
   inorder_search(node->right,val);
}

void insert(int data1, int data2, char subtree){
   struct node *current;
   struct node *temp1;
   struct node *temp2;

   temp1->data= data1;
   temp1->left= NULL;
   temp1->right= NULL;

   temp2->data= data2;
   temp2->left= NULL;
   temp2->right= NULL;

   if(root==NULL){
      root= temp1;
      if(subtree=='R'){
         root->right= temp2;
         return;
      }
      else{
         root->left= temp2;
         return;
      }
   }
   else{
      current= inorder_search(root,data1);
      if(subtree=='R'){
         current->right= temp2;
         return;
      }
      else{
         current->left= temp2;
         return;
       }
     }
   }

void preorder_traversal(struct node *node){
    if(node==NULL)   return;
    cout<<node->data<<" ";
    preorder_traversal(root->left);
    preorder_traversal(root->right);
}

int main(void){
   int edges;//edges= no. of edges
   cout<<"Enter the number of edges: ";
   cin>>edges;

   int i;
   int relations= edges*3;
   int *arr= new int[relations+1]; //since input is in the form 1 2 R 1 2 L where R= right subtree, L= left subtree
   for(i=0; i<relations;i++)  cin>>arr[i];

   for(i=0;i<relations;i+=3) insert(arr[i], arr[i+1], arr[i+2]);

   preorder_traversal(root);
}

代码用于以下输入类型

1 2 L 1 3 R

函数 insert(),我正在尝试为已采用数组形式的输入创建一棵树。函数 inorder_search() 用于搜索要向其左子树或右子树添加数据的特定节点,并将其返回给 insert() 函数。例如在 1 2 L 1 3 R 中,“1”是节点,“2”和“3”分别是左右子树。所以我在 inorder_search() 函数中搜索“1”,并在 insert() 函数中返回相应插入“2”或“3”的节点。

有人可以解释我到底哪里出错了,是否有更好的方法来实现它?

【问题讨论】:

  • 是作业吗?
  • 使用调试器并检查核心转储。它在哪里(stacktrace)崩溃了?
  • 无任何分配。你确定insert?无论哪种方式,这都是 C 而非 C++。
  • 编译:gcc -Wall -Werror -g 调试:gdb ./myprog
  • @Code Bunny 函数 inorder_search 具有无限递归。

标签: c++ binary-tree


【解决方案1】:

看起来您在编译时关闭了警告。当我编译这个时,我收到以下消息:

$ g++ -std=c++14 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Weffc++     38307710.cpp   -o 38307710
38307710.cpp: In function ‘node* inorder_search(node*, int)’:
38307710.cpp:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
38307710.cpp: In function ‘void insert(int, int, char)’:
38307710.cpp:23:22: warning: ‘temp1’ is used uninitialized in this function [-Wuninitialized]
    temp1->data= data1;
                      ^
38307710.cpp:27:22: warning: ‘temp2’ is used uninitialized in this function [-Wuninitialized]
    temp2->data= data2;
                      ^

此外,在 Valgrind 下运行会提供

==5113== Use of uninitialised value of size 8
==5113==    at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113==    by 0x400B77: main (38307710.cpp:72)
==5113== 
==5113== Invalid write of size 4
==5113==    at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113==    by 0x400B77: main (38307710.cpp:72)
==5113==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5113== 
==5113== 
==5113== Process terminating with default action of signal 11 (SIGSEGV)
==5113==  Access not within mapped region at address 0x0
==5113==    at 0x40093C: insert(int, int, char) (38307710.cpp:23)
==5113==    by 0x400B77: main (38307710.cpp:72)

它准确地指示了您首先访问无效值的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多