简体   繁体   English

CPP 中的分段错误(核心转储)

[英]Segmentation fault(core dumped) in CPP

I am confused a bit.我有点困惑。 I narrowed down it to:我把它缩小到:

Why having this line:为什么有这条线:

Node *root, *rootSafe = NULL;

give error:给出错误:

Segmentation fault(core dumped)

While just switching it like below:虽然只是像下面这样切换它:

Node *rootSafe, *root = NULL;

runs perfect.运行完美。

Here is the code, you can test.这是代码,你可以测试。

#include <iostream>
using namespace std;
struct Node{
    int data;
    Node *left, *right;
    Node(int d){
        this->data = d;
        this->left = this->right = NULL;
    }
};
Node *newNode(int d){
    Node *temp = (Node *)malloc(sizeof(Node));
    temp->data = d;
    temp->left = temp->right = NULL;
    return temp;
}
void printInorder(Node *root){
    if(root == NULL){
        return;
    }
    else{
        printInorder(root->left);
        cout << "--" << root->data;
        printInorder(root->right);
    }
}
int main()
{
    //cout << "Hello World";
    Node *rootSafe, *root = NULL;
    int arr[] = {5, 3, 1, 4, 6};
    int sizeArr = sizeof(arr)/sizeof(arr[0]);
    
    for(auto i = 0; i < sizeArr; i++){
        if(root == NULL){
            rootSafe = newNode(arr[i]);
            root = rootSafe;
        }
        else{
            while(root != NULL){
                if(arr[i] < root->data){//Move left
                    if(root->left == NULL){
                        root->left = newNode(arr[i]);
                        root = NULL;
                    }
                    else{
                        root = root->left;
                    }
                }
                else{//Move right
                    if(root->right == NULL){
                        root->right = newNode(arr[i]);
                        root = NULL;
                    }
                    else{
                        root = root->right;
                    }
                }
            }
        }
        root = rootSafe;
    }
    
    cout << "\n Print Inorder: ----"; printInorder(rootSafe);
    return 0;
}

Without fiddling with your code, I think this:在不摆弄您的代码的情况下,我认为:

Node *root, *rootSafe = NULL;

Doesn't do what you think it does.不做你认为它做的事。 Do you think it sets both to NULL?你认为它同时设置为 NULL 吗? It doesn't.它没有。 root gets some random value and rootSafe gets NULL. root 得到一些随机值,rootSafe 得到 NULL。

This might be what you really want:这可能是您真正想要的:

Node *root = NULL, *rootSafe = NULL;

Frankly, I personally hate (and it's against coding conventions at some work places) specifying multiple variables on the same line.坦率地说,我个人讨厌(并且在某些工作场所违反编码约定)在同一行指定多个变量。 You will not find this in my code.你不会在我的代码中找到这个。 Instead, you will see:相反,您会看到:

Node * root = nullptr;
Node * rootSafe = nullptr;

Note also that in modern C++, NULL is not a pointer.另请注意,在现代 C++ 中,NULL 不是指针。 Get in the habit of using nullptr.养成使用 nullptr 的习惯。

You are using root here:你在这里使用root

        if(root == NULL){

Your original declaration您的原始声明

Node *root, *rootSafe = NULL;

doesn't initialize root , leaving it with indeterminate value.不初始化root ,留下不确定的值。 Therefore, root with some random invalid value may be dereferenced and it may lead to Segmentation Fault.因此,具有一些随机无效值的root可能会被取消引用,并可能导致分段错误。

To avoid this, you should initialize root before using that.为避免这种情况,您应该在使用它之前初始化root

It can be done in declaration:它可以在声明中完成:

Node *root = NULL, *rootSafe = NULL;

Or before the loop:或者在循环之前:

    root = NULL;
    for(auto i = 0; i < sizeArr; i++){
Node *root, *rootSafe = NULL;
Node *rootSafe, *root = NULL;

The first line leaves root uninitialized.第一行未初始化 root。 This causes undefined behaviour while access root.这会在访问 root 时导致未定义的行为。

The second line leaves rootSafe uninitialized but the var later initialized rootSafe = newNode(arr[i]);第二行未初始化 rootSafe 但 var 后来初始化rootSafe = newNode(arr[i]); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM