将指针传递给函数并对其进行修改 [英] Passing a Pointer into a Function and Modifying it

查看:84
本文介绍了将指针传递给函数并对其进行修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stackoverflow和C/C ++的新功能.我通过实现二叉树来工作,有一个简单的问题.可以说我有以下内容:

New to both stackoverflow and C/C++. Im working through implementing a binary tree and have a simple question. Lets say I have the following:

struct Node {
    int data;
    Node* right_child;
    Node* left_child;
};

void addNode(Node* tree, int new_data){
    if(tree == NULL){
        Node* new_tree = new Node;
        new_tree->data = new_data;
        new_tree->right_child = NULL;
        new_tree->left_child = NULL;
        tree = new_tree;
    }
}

int main(){
    Node* tree = new Node;
    tree = NULL;
    addNode(tree, 3);
    cout << tree->data << endl; //CRASH
}

非常简单的权利.它将崩溃,因为即使从addNode返回后,树仍将为NULL.我试图理解的是为什么一旦调用addNode就不会对其进行更新.当然,使用并更新了指针的副本,但是它不应该仍保持相同的地址吗?因此,仍然要更新原始内存地址并返回.还是由于某种原因,新指针指向了不同的位置?我对发生的事情感到困惑.任何帮助都会很棒.

Pretty simple right. It would crash because the tree would still be NULL, even after the return from addNode. what I have attempted to understand is why it would not be updated once addNode is called. Sure, a copy of the pointer is used and updated, but shouldn't it still hold the same address? Therefore, still update the original memory address and return. Or does the new pointer for some reason point to a different location? I'm confused about what is going on. Any help would be wonderful.

此外,我只是在网站上编写了该代码-很抱歉,如果没有什么错误,实际上并没有运行它.

Also, i just wrote that code on the website - sorry if there are little errors, didnt actually run it.

谢谢.

推荐答案

有一些问题,

首先,您具有指针 addNode 的本地副本,并且不是取消引用它以对其所指向的对象进行操作,而是直接对本地指针本身进行操作.在编<代码>节点该函数只是失去永远内部创建的.

First, you have a local copy of the pointer addNode, and you are not de-referencing it to act on the thing it points to, but acting directly on the local pointer itself. The newed Node created inside the function is just lost forever.

您可以通过按引用传递指针来解决该问题.这不需要对您的代码进行任何其他修改:

You can fix that problem by passing the pointer by reference. This doesn't require any other modifications to your code:

void addNode(Node*& tree, int new_data)

第二,正如您所指出的,您正在 main 中取消引用NULL指针.这就是未定义的行为(UB).可能发生的事情之一就是崩溃.但是代码可以安静地运行而不会崩溃.重要的事实是它是UB,并且无法依赖该程序.

Second, as you noted, you are de-referencing a NULL pointer in main. This is simply undefined behaviour (UB). One of the things that can happen is a crash. But the code could just run silently without crashing too. The important fact is that is it UB and the program cannot be relied on.

注释1 :如果要初始化 Node ,以便将数据成员初始化为零,请使用值初始化:

Note 1: If you want to initialize your Nodes such that the data members are zero initialized, use value initialization:

Node* tree = new Node();

注释2 :使用原始的 new 指针要非常小心.您的代码中已经有一个 两个资源泄漏.最好使用最合适的智能指针类型.

Note 2: Be very careful with using raw newed pointers. You already have one two resource leaks in your code. Better use the most appropriate type of smart pointer.

这篇关于将指针传递给函数并对其进行修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆