首页 > 解决方案 > 在 C++ 中使用引用传递指针时出错

问题描述

我正在尝试使用引用来理解指针的传递,并且我有一个代码如下

// { Driver Code Starts
#include <bits/stdc++.h>

using namespace std;

 // } Driver Code Ends
//User function template for C++

class Node{
       
    public:
        int val;
        Node *left,*right;
        int count;
        
        Node(int key,int c){
            this->val=key;
            this->count=c;
            this->left=this->right=NULL;
        }
};
    
class NodeInsert{
public:
    int insert(Node*& node,int key,int countSmaller){

        if(node==NULL){
            node=new Node(key,0);
            return countSmaller;
        }
        
        if(key<node->val){
            node->count++;
            return insert(node->left,key,countSmaller);
        }
        else{
            if(key>node->val){
                return node->count+insert(node->right,key,countSmaller+1);
            }
        }
    }
    
    void temp(){
    cout<<"hello world"<<"\n";
    }
};

class Solution{
public: 
    vector<int> constructLowerArray(int *arr, int n) {

        vector<int> ans;
        Node *root=NULL;
        
        NodeInsert *n_ins=new NodeInsert();
        for(int i=n-1;i>=0;i--){
            ans[i]=n_ins->insert(root,arr[i],ans[i]);
        }

        
        return ans;
    }
};
    

    


// { Driver Code Starts.

int main() {
    int n=4;
    int arr[n]={12,3,2,1};
    Solution *ob=new Solution();
    auto ans = ob->constructLowerArray(arr, n);
        
        for (auto x : ans) {
            cout << x << " ";
        }
        cout << "\n";
    return 0;
}

我执行了上面的代码并得到了分段错误,为了找出导致分段错误的行,我必须按照以下步骤操作:

使用编译代码

a) gcc -g tree.cpp -lstdc++

b) gdb a.exe core

then press r

它返回导致分段错误的行号

GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\workarea\cpp\a.exe...done.
C:\workarea\cpp/core: No such file or directory.
(gdb) r
Starting program: C:\workarea\cpp/a.exe
[New Thread 10728.0x3614]
[New Thread 10728.0x33ec]
[New Thread 10728.0x5ae8]
[New Thread 10728.0x724]

Program received signal SIGSEGV, Segmentation fault.
0x00407c7e in Solution::constructLowerArray (this=0x821818, arr=0x63feb0, n=4, root=@0x63fee4: 0x0) at tree.cpp:57
57                      ans[i]=n_ins->insert(root,arr[i],ans[i]);

我的代码中的第 57 行是

ans[i]=n_ins->insert(root,arr[i],ans[i]);

我正在使用的 gcc 版本是

gcc --version
gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

并使用以下链接

  1. C++ 对非常量的引用初始值必须是左值

  2. 通过引用传递指针时出错

我开始知道在 c++ 中我们不能将引用分配给临时变量

但我不明白我的代码中的临时变量是什么?它是根吗?

但是下面的代码(从 geeksforgeeks 复制)运行良好,没有任何问题

#include<bits/stdc++.h>
using namespace std;

// BST node structure
class Node{
    
public:
    int val;
    int count;
    Node* left;
    Node* right;
    
    // Constructor
    Node(int num1, int num2)
    {
        this->val = num1;
        this->count = num2;
        this->left = this->right = NULL;
    }
};

// Function to addNode and find the smaller
// elements on the right side
int addNode(Node*& root, int value,
                        int countSmaller)
{
    
    // Base case
    if (root == NULL)
    {
        root = new Node(value, 0);
        return countSmaller;
    }
    if (root->val < value)
    {
        return root->count +
    addNode(root->right,
            value,
            countSmaller + 1);
    }
    else
    {
        root->count++;
        return addNode(root->left, value,
                    countSmaller);
    }
}

// Driver code
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int data[] = { 10, 6, 15, 20, 30, 5, 7 };
    int size = sizeof(data) / sizeof(data[0]);
    int ans[size] = {0};

    Node* root = NULL;
    
    for(int i = size - 1; i >= 0; i--)
    {
        ans[i] = addNode(root, data[i], 0);
    }

    for(int i = 0; i < size; i++)
        cout << ans[i] << " ";

    return 0;
}

我找不到我的代码和来自 geeksforgeeks 的代码之间的区别。究竟是什么导致了我的代码中的分段错误?

标签: c++pointerssegmentation-faultbinary-treebinary-search-tree

解决方案


推荐阅读