函数中使用的内存什么时候会变得空闲?(C 编程)

发布于 2024-12-07 11:20:10 字数 688 浏览 2 评论 0原文

下面是代码

代码:

#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;
}

问题:

  1. 我们知道,函数 num 对其函数 num 来说是局部的() ,所以在这段代码中我尝试将函数中变量 num 的地址返回给调用它的函数,即 main() .

  2. ,我只需使用解引用运算符来提取特定 num 变量的值,并在 main() 函数中将其打印出来。

  3. 有一件事我很困惑。我记得我读过一本关于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 :

  1. As we know , the function num is local to its function num() , so in this code I try to return the address of the variable num in the function to the function that calls it , which is main().

  2. After that I just use the dereferencing operator to extract the value of the specific num variable and print it out in main() function.

  3. 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 技术交流群。

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

发布评论

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

评论(3

等风来 2024-12-14 11:20:10

您可以看到变量值的原因是堆栈的工作方式。实际上,当您输入函数 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.

关于从前 2024-12-14 11:20:10

只是添加@Goz 已经很好解释的内容。试试这个代码:

#include <stdio.h>

int * num(void);

int fib(int n)
{
    if( 0 == n || 1 == n )
        return n;

    return fib(n-1) + fib(n-2);
}

int main(void)
{
    int * num2;
    num2 =num();
    (void)fib(7);
    printf("%d\n" , *num2);

    return 0;
}


int * num(void)
{
    int num = 20;

    return #
}

很有可能你不会得到“20”作为输出(也有可能程序终止)
此外,当您编译时,编译器会警告您有关此“警告:函数返回局部变量的地址”:)

Just adding to what @Goz has very well explained. Try this code:

#include <stdio.h>

int * num(void);

int fib(int n)
{
    if( 0 == n || 1 == n )
        return n;

    return fib(n-1) + fib(n-2);
}

int main(void)
{
    int * num2;
    num2 =num();
    (void)fib(7);
    printf("%d\n" , *num2);

    return 0;
}


int * num(void)
{
    int num = 20;

    return #
}

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" :)

﹂绝世的画 2024-12-14 11:20:10

返回指向局部变量的指针是不正确的。当该函数返回时,变量 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 value 20. The C runtime doesn't fill that part of memory with 0's after deallocation.

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