从 C 函数返回局部变量

发布于 2024-10-14 09:59:18 字数 666 浏览 2 评论 0原文

#include <stdio.h>

int foo1(void)
{
    int p;
    p = 99;
    return p;
}

char *foo2(void)
{
    char buffer[] = "test_123";
    return buffer;
}

int *foo3(void)
{
    int t[3] = {1,2,3};
    return t;
}

int main(void)
{
    int *p;
    char *s;

    printf("foo1: %d\n", foo1());
    printf("foo2: %s\n", foo2());
    printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
    return 0;
}

当我使用 gcc -ansi -pedantic -W -Wall 编译它时,编译器会发出 foo2() 和 foo3() 的警告消息:

warning: function returns address of local variable

我认为不允许返回局部变量,但是 foo1( )工作正常,并且返回指向本地对象的指针和对象本身之间似乎存在巨大差异。

有人能解释一下这个问题吗?提前致谢!

#include <stdio.h>

int foo1(void)
{
    int p;
    p = 99;
    return p;
}

char *foo2(void)
{
    char buffer[] = "test_123";
    return buffer;
}

int *foo3(void)
{
    int t[3] = {1,2,3};
    return t;
}

int main(void)
{
    int *p;
    char *s;

    printf("foo1: %d\n", foo1());
    printf("foo2: %s\n", foo2());
    printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
    return 0;
}

When I compile this with gcc -ansi -pedantic -W -Wall the compiler issues warning messages for foo2() and foo3():

warning: function returns address of local variable

I thought it is not allowed to return a local variable, but foo1() works fine and it seems there is a huge difference between returning pointer to a local object and the object itself.

Could anybody shed some light on this issue? Thanks in advance!

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

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

发布评论

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

评论(4

蓝咒 2024-10-21 09:59:18

这里的问题是,当您创建局部变量时,它会在堆栈上分配,因此一旦函数完成执行就不可用(此处的实现有所不同)。更好的方法是使用 malloc() 来保留非本地内存。这里的危险在于,您必须释放 (free()) 使用 malloc() 分配的所有内容,如果忘记,就会造成内存泄漏。

The issue here is that when you create the local variable it is allocated on the stack and is therefore unavailable once the function finishes execution (implementation varies here). The preferable way would be to use malloc() to reserve non-local memory. the danger here is that you have to deallocate (free()) everything you allocated using malloc(), and if you forget, you create a memory leak.

凤舞天涯 2024-10-21 09:59:18

对于foo1(),您返回局部变量的副本,而不是局部变量本身。

对于其他函数,您返回指向局部变量的指针的副本。然而,当函数完成时,该局部变量将被释放,因此如果您稍后尝试引用它,您最终会遇到严重的问题。

For foo1(), you return a copy of the local variable, not the local variable itself.

For the other functions, you return a copy of a pointer to a local variable. However, that local variable is deallocated when the function finishes, so you end up with nasty issues if you try to reference it afterwards.

爱情眠于流年 2024-10-21 09:59:18

任何变量在内存中都有一定的空间。指针引用该空间。当函数调用返回时,局部变量占用的空间将被释放,这意味着它可以并且将会被重用于其他事情。因此,对该空间的引用最终将指向一些完全不相关的东西。 C 中的数组是作为指针实现的,因此这最终适用于它们。在函数中声明的常量数组也算作本地常量数组。

如果要使用超出创建它的函数范围的数组或其他指针,则需要使用 malloc 为其保留空间。使用 malloc 保留的空间不会被重新分配或重用,直到通过调用 free 显式释放为止。

Any variable has some space in the memory. A pointer references that space. The space that local variables occupies is deallocated when the function call returns, meaning that it can and will be reused for other things. As a consequence, references to that space are going to wind up pointing to something completely unrelated. Arrays in C are implemented as pointers, so this winds up applying to them. And constant arrays declared in a function also count as being local.

If you want to use an array or other pointer beyond the scope of the function in which it is created, you need to use malloc to reserve the space for it. Space reserved using malloc will not be reallocated or reused until it is explicitly released by calling free.

滿滿的愛 2024-10-21 09:59:18

是的,您正在返回一个数组,它实际上是幕后的指针,指向存储您初始化的变量内容的内存位置的地址。因此,它警告您,当您实际上可能指的是数组值之一时,返回这样的结果可能不太有用。

Yes you are returning an array, which is actually a pointer behind the scenes, to the address of the memory location where the contents of the variable you've initialised is stored. So it's warning you that it might not be quite as useful to return such a result, when you might really mean one of the array values instead.

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