双指针和本地范围混乱

发布于 2025-01-18 13:41:21 字数 789 浏览 8 评论 0原文

我正在 Udemy 上学习 ac 编程课程,并且在将双指针传递给函数时感到非常困惑。在示例中,讲师将指针的地址作为参数传递给函数。然后,他取消引用函数中的双指针参数并将其设置为等于局部变量 (a) 的地址。

#include <stdio.h>
#include <malloc.h>

void foo(int **temp_ptr)
{
   int a = 5;
   *temp_ptr = &a;
}

int main()
{
   int *ptr = NULL;
   ptr = (int*) malloc(sizeof(int));
   *ptr = 10;
   foo(&ptr);
   printf("%d\n", *ptr);

   return 0;
}

因此,我们在这里所做的就是更改 ptr 的值以保存 a 的地址。然后,当我们取消引用 ptr 时,它应该显示 5 的值,而不是 10,因为我们更改了 的值ptr 保存a 的地址。

这确实是正确的:它显示 5 而不是 10。

但是,这里没有意义的是变量 a 位于 foo 函数的本地范围内。当我们声明a时,分配的内存被放入堆栈帧中;因此,当离开函数的本地作用域时,内存将被删除,并且帧将从堆栈中弹出。我们怎样才能在主函数中使用ptr变量来访问变量a呢?那段记忆不应该被彻底抹去吗?

I am taking a c programming course on Udemy and am quite confused when passing a double pointer into a function. In the example, the instructor passes the address of a pointer as an argument to a function. Then, he de-references that double pointer parameter in the function and sets it equal to the address of a local variable (a).

#include <stdio.h>
#include <malloc.h>

void foo(int **temp_ptr)
{
   int a = 5;
   *temp_ptr = &a;
}

int main()
{
   int *ptr = NULL;
   ptr = (int*) malloc(sizeof(int));
   *ptr = 10;
   foo(&ptr);
   printf("%d\n", *ptr);

   return 0;
}

So, what we are doing here is changing the value of ptr to hold the address of a. Then, when we de-reference ptr, it should display the value of 5 and not 10, since we changed the value of ptr to hold the address of a.

This is indeed correct: it displays 5 and not 10.

However, what doesn't make sense here is that the variable a is in the local scope of the foo function. When we declare a, the memory allocated is put onto the stack frame; thus, when leaving the local scope of the function that memory is deleted and the frame is popped off of the stack. How can we still access that variable a with the ptr variable in the main function? Shouldn't that memory be completely wiped out?

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

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

发布评论

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

评论(2

苏璃陌 2025-01-25 13:41:21

然后,当我们退还参考ptr时,它应显示5

的值

的值是不正确的。 printf(“%d \ n”, *ptr); 不确定的行为(ub),因为ptr中存储的地址无效foo(&amp; ptr);的返回。

输出可能打印5,可能打印42,可能崩溃。是UB。

...纠正堆栈的内存应被完全删除

。没有指定的内存擦除。结果是UB。

但是有时不确定的行为有效吗?

“未定义的行为”是不确定的。即使今天“有效”,明天也可能不会“工作”。

当我们声明A时,分配的内存将放在堆栈框架上;因此,当离开函数的本地范围时,将内存被删除并从堆栈中弹出框架。

假定 一个未由C指定的基础代码模型。

我们如何仍然可以在主函数中使用ptr变量访问该变量?

如今,没有定义的方式。或者,通过制作a static来保持a的地址有效。

Then, when we de-reference ptr, it should display the value of 5

Should is incorrect. printf("%d\n", *ptr); is undefined behavior (UB) as the address stored in ptr is invalid with the return of foo(&ptr);.

Output may print 5, may print 42, may crash. It is UB.

... correct that the memory of the stack should be completely wiped out

No. There is not specified wiping of memory. The result is UB.

But sometimes undefined behavior works?

"undefined behavior" is undefined. Even if it "works" today, it may not "work" tomorrow.

When we declare a, the memory allocated is put onto the stack frame; thus, when leaving the local scope of the function that memory is deleted and the frame is popped off of the stack.

This assumes an underling code model that is not specified by C. Other real possibilities exist.

How can we still access that variable a with the ptr variable in the main function?

As is, no defined way. Alternatively keep the address of a valid by making a static.

流年里的时光 2025-01-25 13:41:21

在 Foo 函数中,我们正在更新指针的地址。 foo 函数中声明的 int a 将存储在某个内存位置。现在,我们将该位置分配给 ptr(没关系,ptr 可以指向 int 类型的任何内存位置)。但问题是该位置的值不在我们的控制范围内,并且该位置可以用于任何其他目的,这可能会更新该位置的值。

这意味着我们的指针现在指向可能由任何其他源更新的位置。

In Foo function, we are updating the address of our pointer. int a declared in foo function will be stored at some memory location. Now, we are assigning that location to our ptr(which is okay, ptr can point to any memory location of type int). But the issue is the value at that location is not in our control and this location can be used for any other purpose which may update the value at that location.

It means that our pointer is now pointing to location that may be updated by any other source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文