簡體   English   中英

為什么我的代碼顯示錯誤細分錯誤(核心已轉儲)

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

我試圖在二叉樹中實現預遍歷。 這是我的代碼段。

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

此外,在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)

准確指示您首次訪問無效值的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM