关于返回函数中本地指针变量的问题
我知道功能中的变量是使用堆栈空间。当功能退出时,空间将被释放。这就是为什么我们应该将指针变量声明为函数中的静态。但是,我发现下面的代码效果很好。
GCC版本为:GCC版本9.3.0(Ubuntu 9.3.0-17ubuntu1〜20.04)
#include <stdio.h>
char *month_name(int n) {
char *name[] = {"Invalid name", "Jan.", "Feb", "Mar", "Apr", "May", "June",
"July", "Aug", "Sep", "Oct", "Nov", "Dec"};
return n < 1 || n > 12 ? name[0] : name[n];
}
int main() {
char *month;
month = month_name(2);
printf("%s\n", month); // The output is Feb
}
似乎函数中的变量被隐含地转换为静态。有人可以为我解释吗?提前致谢。
I know the variables in function are using stack space. When function exit, the space are freed. That's why we should declare the pointer variable as static in function. However, I found that the code below works well.
The gcc version is: gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
#include <stdio.h>
char *month_name(int n) {
char *name[] = {"Invalid name", "Jan.", "Feb", "Mar", "Apr", "May", "June",
"July", "Aug", "Sep", "Oct", "Nov", "Dec"};
return n < 1 || n > 12 ? name[0] : name[n];
}
int main() {
char *month;
month = month_name(2);
printf("%s\n", month); // The output is Feb
}
It seems the variable in function is translate to static implicitly. Can anyone explain for me? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会在此处返回本地数据。本地数据是数组。它包含对字符串文字的指针,这些指针仅存储在恒定读取内存中。他们的位置或寿命不会改变。因此,可以将指针返回给他们是可以的。
但是,如果您试图将指针返回到阵列的指针,那是错误的。
You’re not returning local data here. The local data is the array. It contains pointers to string literals, which are stored in constant read only memory. Their location or lifetime does not change. So it is ok to return a pointer to them.
If you tried to return a pointer to the array, however, that would be wrong.
您将一系列指针宣布为字符串文字(对于他们的第一个字符)
字符串文字具有静态存储持续时间。那就是他们退出功能后还活着。
例如,在C标准(6.4.5字符串文字)中有书面
另一方面,阵列本身具有自动存储持续时间,在退出函数后,它没有活着。但是该函数将指针返回到字符串文字而不是指向数组本身的指针。
如果您试图将指针返回到数组本身(例如
,以下方式)
,或者如果您会声明一个二维数组,则
该
函数将是不正确 的退出功能后,阵列本身将不会活着。
在与C字符串文字相反的C ++中,请注意具有恒定字符阵列的类型。因此,要将您的函数编译为C ++代码,您必须
在C中以以下方式定义函数,以相同的方式定义函数要好得多,因为尽管在C字符串中具有非恒定字符的类型,但仍有任何尝试更改字符串字面调用不确定的行为。这样的函数定义允许避免程序错误。
You declared an array of pointers to string literals (to their first characters)
String literals have static storage duration. That is they are alive after exiting the function.
For example in the C Standard (6.4.5 String literals) there is written
On the other hand, the array itself has automatic storage duration that is it is not alive after exiting the function. But the function returns a pointer to a string literal instead of a pointer to the array itself.
The function would be incorrect if you tried to return a pointer to the array itself as for example
or the following way
Or if you would declare a two dimensional array like
then in this case the return statement
indeed would invoke undefined behavior by the same reason that the array itself will not be alive after exiting the function.
Pay attention to that in C++ opposite to C string literals have types of constant character arrays. So to compile your function as a C++ code you have to define the function the following way
Also in C it is much better to define the function the same way because though in C string literals have types of non-constant character arrays nevertheless any attempt to change a string literal invokes undefined behavior. Such a function definition allows to avoid program bugs.