简体   繁体   English

为什么和如何返回静态变量的地址可以,而对非静态变量执行相同的操作却得到错误的结果?

[英]Why and how returning the address of a static variable is OK while doing the same with non-static variable gives wrong result?

char str[] = "hello";

This definition dosn't work but 这个定义不起作用,但是

static char str[] = "hello" 

This works, why? 这行得通,为什么呢?

Full code: 完整代码:

char *retstr(void)
    {
        char str[] = "hello"; //This definition dosnt work
        return str;
    }

    int main()
    {
        printf("\n%s\n",retstr());
        return 0;
    }

In case of 的情况下

 char str[] = "hello"; //This definition dosnt work

you're trying to return the address of a local variable str . 您正在尝试返回局部变量str的地址。 Using the returned value is undefined behaviour here, because once the retstr() function finishes execution, there is no existence of the str variable. 这里使用返回值是未定义的行为 ,因为一旦retstr()函数完成执行,就不会存在str变量。

OTOH, 太太

 static char str[] = "hello";

works, because, the static variable as static storage duration . 之所以有效,是因为static变量作为static storage duration Its lifetime is the entire execution of the program. 它的生命周期是程序的整个执行过程。 So even after the retstr() function finishes execution, str remains valid, and hence, the return value is valid to be used. 因此,即使在retstr()函数完成执行后, str仍然有效,因此返回值有效。

Related: C11 , chapter §6.2.4, Storage durations of objects , Paragraph 3 相关: C11 ,第§6.2.4章, 对象的存储时间 ,第3段

... with the storage-class specifier static , has static storage duration . ......与存储类说明static ,具有静态存储时间 Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup. 它的生命周期是程序的整个执行过程,并且在程序启动之前,它的存储值仅初始化一次。

char str[] = "hello"; //This definition dosnt work
return str;

Here you are returning the local address. 在这里,您将返回本地地址。 that's why it is not working 这就是为什么它不起作用

static char str[] = "hello" 

while in this one, you are changing the scope of the 'str' variable to the whole program. 在此过程中,您正在将'str'变量的范围更改为整个程序。 that's why it works. 这就是为什么它起作用。

You cannot return local variables from a function. 您不能从函数返回局部变量。 Local variables live on the stack, and once the function completes, that stack space is released and can be used by the next function call. 局部变量存在于堆栈中,一旦函数完成,该堆栈空间将被释放,并可由下一个函数调用使用。

char str[] = "hello"; //this won't work as it is a local variable

Once you come out of the scope there doesn't exist any variable named str for other function calls. 一旦超出范围,就不会再有其他函数调用名为str变量。

But static variable lifetime extends across the entire run of the program. 但是static变量的生存期遍及整个程序。

static char str[] = "hello"; //this will work

Hence on using static it is not giving any error. 因此,在使用static它不会给出任何错误。

Static variables are created once and does not get destroyed when a function returns but local variables gets destroyed once our function ends. 静态变量仅创建一次,并且在函数返回时不会被销毁,而局部变量在函数结束时会被销毁。
local variable are created every time a function is called and gets destroyed when it returns. 每次调用函数时都会创建局部变量,并在返回时销毁该局部变量。

In case of local variable : you are returning a pointer to a string which exists no-more. 如果是局部变量:您将返回一个指针,该指针不再存在。
In case of global variable : the string is still there and hence it will always work. 如果是全局变量:字符串仍然存在,因此它将始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM