函数中使用的内存什么时候会变得空闲?(C 编程)
下面是代码
代码:
#include <stdio.h>
int * num(void);
int main(void)
{
int * num2;
num2 =num();
printf("%d\n" , *num2);
return 0;
}
int * num(void)
{
int num = 20;
return #
}
问题:
我们知道,函数
num
对其函数num 来说是局部的()
,所以在这段代码中我尝试将函数中变量num
的地址返回给调用它的函数,即main()
.- ,我只需使用解引用运算符来提取特定
num
变量的值,并在main()
函数中将其打印出来。 有一件事我很困惑。我记得我读过一本关于javascript的书,其中提到变量的生存期是在函数内,这意味着在函数完成执行其指令并将控制权传递回调用它的函数后,函数中每个变量的值将是干净的但是为什么在这段代码中我的
main()
函数仍然可以指向该特定内存地址的值?
Below is the code
The Code:
#include <stdio.h>
int * num(void);
int main(void)
{
int * num2;
num2 =num();
printf("%d\n" , *num2);
return 0;
}
int * num(void)
{
int num = 20;
return #
}
The Question :
As we know , the function
num
is local to its functionnum()
, so in this code I try to return the address of the variablenum
in the function to the function that calls it , which ismain()
.After that I just use the dereferencing operator to extract the value of the specific
num
variable and print it out inmain()
function.There's one thing i'm confused . I remember i read a book about javascript that mention a variable lifetime is within the function , which mean after the function finish performing its instructions and pass the control back to the function that calls it , the value of each variable in the function will be clean out(garbage collector).But why in this code my
main()
function still can point to the value of that specific memory address??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以看到变量值的原因是堆栈的工作方式。实际上,当您输入函数
num
时,指针(堆栈指针)就会被移动,以为函数的本地存储添加空间。当您退出函数时,堆栈指针实际上会向后移动,这意味着下一个函数调用将覆盖上一个函数调用中使用的堆栈存储。然而,在被覆盖之前,该值一直处于一种不确定状态。它实际上仍然存在于内存中,但随时可能被覆盖。实际价值的存在可能是这样,也可能不是。这就是为什么按照上面的做法被称为未定义行为。基本上...不要这样做。
The reason you can see the value of the variable is because of how the stack works. Effectively as you enter the function
num
, a pointer (the stack pointer) is moved to add space in for the local storage of the function. When you exit the function the stack pointer is moved back effectively meaning that the next function call will overwrite the stack storage used in the previous function call. Until it is overwritten, however, the value exists in a sort of limbo. It's still actually there in memory but may get overwritten at any moment. The existence of the actual value there may or may not be the case. That is why doing as you do above is known as undefined behaviour.Basically ... don't do it.
只是添加@Goz 已经很好解释的内容。试试这个代码:
很有可能你不会得到“20”作为输出(也有可能程序终止)
此外,当您编译时,编译器会警告您有关此“警告:函数返回局部变量的地址”:)
Just adding to what @Goz has very well explained. Try this code:
There is very good chance that you will not get "20" as the output (There is a chance of program termination as well)
Also when you are compiling the compiler does warn you about this "warning: function returns address of local variable" :)
返回指向局部变量的指针是不正确的。当该函数返回时,变量 num 被释放,并且该内存地址可以自由地由 C 运行时用于另一个值。返回的指针现在无效,因为它指向未分配的内存区域,并且如果使用,将调用未定义的行为。未定义的行为意味着它可能会或可能不会执行您想要的操作。在某些情况下程序会崩溃,在其他情况下它可能会工作。它在这种情况下起作用的原因是因为该部分内存仍将保存值
20
。释放后,C 运行时不会用 0 填充该内存部分。Returning a pointer to a local variable is not right. The variable
num
is deallocated when that function returns and that memory address is free to be used by the C runtime for another value. The pointer returned is now invalid, because it's pointing to an unallocated region of memory, and will invoke undefined behaviour if used. Undefined behaviour means it may or may not do what you want. In some cases the program will crash, in other case it may work. Why it works in this case is because that part of memory will still hold the value20
. The C runtime doesn't fill that part of memory with 0's after deallocation.