在 C++ 中,传递给函数的指针的更改是否反映在调用函数中?

发布于 2024-09-18 22:12:55 字数 346 浏览 2 评论 0原文

如果我将指针P从函数f1传递给函数f2,并在f2中修改P的内容,这些修改是否会自动反映在f1中?

例如,如果我需要删除链表中的第一个节点:

void f2( Node *p)
{
    Node *tmp = p;
    p = p -> next;
    delete tmp;
}

对 P 所做的更改是否会反映在调用函数中,还是现在指向已释放的内存空间?

(我的直觉答案是否定的,更改没有反映出来。但是,好的消息来源告诉我上面的代码可以工作。有人可以花时间给出答案并解释其背后的原因吗?另外,如果上面的代码有问题,我们如何在不使用返回类型的情况下实现这一点?)

If I pass a pointer P from function f1 to function f2, and modify the contents of P in f2, will these modifications be reflected in f1 automatically?

For example, if I need to delete the first node in a linked list:

void f2( Node *p)
{
    Node *tmp = p;
    p = p -> next;
    delete tmp;
}

Will the changes made to P be reflected in the calling function, or will it now point to a memory space that has been deallocated?

( My intuitive answer here is no, changes are not reflected. However, good sources tell me that the above code will work. Can someone take the time to give the answer and explain the reasoning behind it also please? Also, if the above code is faulty, how can we achieve this without using a return type? )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-09-25 22:12:55

如果我将指针P从函数f1传递给函数f2,并在f2中修改P的内容,这些修改会自动反映在f1中吗?

不会。由于 C 和 C++ 实现按值传递,因此指针的值在传递给函数时会被复制。仅修改本地副本,并且在保留函数时不会将值复制回来(这将是少数语言支持的按引用复制)。

如果你想修改原始值,你需要传递一个引用给指针,即将你的f2签名改为:

void f2( Node*& p)

但是,您的 f2 不仅会更改指针 p,还会通过使用 delete 更改其基础值(= 所指向的值)。 这个变化对于调用者来说确实是可见的。

If I pass a pointer P from function f1 to function f2, and modify the contents of P in f2, will these modifications be reflected in f1 automatically?

No. Since C and C++ implement pass-by-value, the value of the pointer is copied when it’s passed to the function. Only that local copy is modified, and the value is not copied back when the function is left (this would be copy-by-reference which a few languages support).

If you want the original value to be modified, you need to pass a reference to the pointer, i.e. change your signature of f2 to read:

void f2( Node*& p)

However, your f2 not only changes the pointer p, it also changes its underlying value (= the value being pointed to) by using delete. That change is indeed visible to the caller.

云裳 2024-09-25 22:12:55

这会起作用,尽管不是你想要的方式。

如果你这样称呼它:

Node* mynode = new Node(); /*fill data*/
f2(mynode);

->将会出现 mynode 的内容未定义。

它将把指针的值传递给另一个函数,并且值(p)对于函数f2来说将是局部的。如果你想修改它,请使用以下方法:

Node* mynode = new Node(); /*fill data*/
f2(&mynode);

void f2(Node** p)
{
 Node* tmp = *p;
 *p = (*p)->next;
 delete tmp;
}

This will work, althrough not the way you want to.

If you call it like:

Node* mynode = new Node(); /*fill data*/
f2(mynode);

-> there will be the content of mynode undefined.

it will pass the value of the pointer to another function, and the value (p) will be local for function f2. If you want to modify it, use this way:

Node* mynode = new Node(); /*fill data*/
f2(&mynode);

void f2(Node** p)
{
 Node* tmp = *p;
 *p = (*p)->next;
 delete tmp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文