双指针和本地范围混乱
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
的值是不正确的。
printf(“%d \ n”, *ptr);
是 不确定的行为(ub),因为ptr
中存储的地址无效foo(&amp; ptr);
的返回。输出可能打印5,可能打印42,可能崩溃。是UB。
。没有指定的内存擦除。结果是UB。
“未定义的行为”是不确定的。即使今天“有效”,明天也可能不会“工作”。
此假定 一个未由C指定的基础代码模型。
如今,没有定义的方式。或者,通过制作
a
static
来保持a
的地址有效。Should is incorrect.
printf("%d\n", *ptr);
is undefined behavior (UB) as the address stored inptr
is invalid with the return offoo(&ptr);
.Output may print 5, may print 42, may crash. It is UB.
No. There is not specified wiping of memory. The result is UB.
"undefined behavior" is undefined. Even if it "works" today, it may not "work" tomorrow.
This assumes an underling code model that is not specified by C. Other real possibilities exist.
As is, no defined way. Alternatively keep the address of
a
valid by makinga
static
.在 Foo 函数中,我们正在更新指针的地址。
foo
函数中声明的int a
将存储在某个内存位置。现在,我们将该位置分配给 ptr(没关系,ptr 可以指向int
类型的任何内存位置)。但问题是该位置的值不在我们的控制范围内,并且该位置可以用于任何其他目的,这可能会更新该位置的值。这意味着我们的指针现在指向可能由任何其他源更新的位置。
In
Foo
function, we are updating the address of our pointer.int a
declared infoo
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 typeint
). 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.