簡體   English   中英

Clion:進程以退出代碼 139 結束

[英]Clion : Process finished with exit code 139

我正在 C++ 中編寫拼貼練習程序,但我不知道為什么會看到此錯誤

進程以退出代碼 139 結束(被信號 11 中斷:SIGSEGV)

僅用於測試 class 並不完整

如果我刪除add('a'); 程序運行成功,但任何 function 調用都會出現此錯誤!

問題是以括號/括號形式讀取一棵樹並以鏈表形式制作一棵樹

#include<iostream>

using namespace std;

class Tree
{
public:
    char data;
    Tree *right;
    Tree *left;
};

Tree *root;
Tree *temp;

void add(char c)
{
    root->data = c;
}

void addR(char c)
{
    temp = new Tree;
    temp->data = c;
    root->right = temp;
}

void addL(char c)
{
    temp = new Tree;
    temp->data = c;
    root->left = temp;
}

int main()
{
    cout<<2;
    add('a');
    return 0;
}

Tree *root;
void add(char c)
{
    root->data = c;
}
int main()
{
    cout<<2;
    add('a'); // <---
    return 0;
}

add('a')通過訪問root->data = c;中未初始化的指針root來調用未定義的行為; . 你應該先初始化它:

root = new Tree();

暫無
暫無

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

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