为什么我的代码显示错误分段错误(核心转储)

Why is my code showing error Segmentation fault(core dumped)

本文关键字:错误 核心 转储 分段 代码 显示 为什么 我的      更新时间:2023-10-16

我试图在二叉树中实现预序遍历。这是我的代码片段。

#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"的节点。

有人可以解释一下我到底哪里出了问题,是否有更好的方法来实施它?

看起来您在关闭警告的情况下编译。 当我编译它时,我收到以下消息:

$ 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;
                      ^

此外,在瓦尔格林德的带领下跑步会给

==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)

这精确地指示您首次访问无效值的位置。