f****4 发帖数: 1359 | 1 Explain the difference between the following two functions, highlight any
perceived problems, and state which one is the preferred implementation and
why it is preferred.
char *function1(void)
{
char *s = "12345";
return s;
}
char *function2(void)
{
char s[MAX_STRLEN] = "12345";
return s;
}
这有啥区别啊? |
y*c 发帖数: 904 | 2 1. returned a pointer to a literal string, which is in the static memory
zone. Safe. But it should return const char*
2. returned a pointer to a local memory, which will be released after the
call. Dangerous. |
f*******r 发帖数: 1086 | 3 Great answer:)
const char *function1(void)
{
const char *s = "12345";
return s;
}
the function1 should be like this.
【在 y*c 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 1. returned a pointer to a literal string, which is in the static memory : zone. Safe. But it should return const char* : 2. returned a pointer to a local memory, which will be released after the : call. Dangerous.
|
y**i 发帖数: 1112 | 4 问个问题,第二个函数里面的文字常量是不是也会在静态区里创建内存,然后再拷贝到
栈里的数组,还是根本就不存在这样一个文字常量?还有就是这个文字常量的作用域怎
样?
【在 y*c 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 1. returned a pointer to a literal string, which is in the static memory : zone. Safe. But it should return const char* : 2. returned a pointer to a local memory, which will be released after the : call. Dangerous.
|
r****o 发帖数: 1950 | 5 刚才试了一下,两个const按存在不存在取4种组合,函数都能正确工作,除了一些
warning以外。
const char *function1(void)
{
const char *s = "12345";
return s;
}
const char *function2(void)
{
char *s = "12345";
return s;
}
char *function1(void)
{
const char *s = "12345";
return s;
}
char *function1(void)
{
char *s = "12345";
return s;
}
是不是这4个函数等价?
【在 f*******r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Great answer:) : const char *function1(void) : { : const char *s = "12345"; : return s; : } : the function1 should be like this.
|
y*c 发帖数: 904 | 6
I think you are right. It exists. I guess it's globally accessible. So if
"12345" is accessed at another place. It should have the same address. I
am not 100% sure about this.
【在 y**i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 问个问题,第二个函数里面的文字常量是不是也会在静态区里创建内存,然后再拷贝到 : 栈里的数组,还是根本就不存在这样一个文字常量?还有就是这个文字常量的作用域怎 : 样?
|
f*******r 发帖数: 1086 | 7 我觉得不是,应该是test的函数本身很简单,
stack空间没有被occupy,返回一个local的指针变量
应该是不safe的
【在 r****o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 刚才试了一下,两个const按存在不存在取4种组合,函数都能正确工作,除了一些 : warning以外。 : const char *function1(void) : { : const char *s = "12345"; : return s; : } : const char *function2(void) : { : char *s = "12345";
|
f*******r 发帖数: 1086 | 8 恩,其实看VC的asm code应该就知道了:)
【在 y*c 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I think you are right. It exists. I guess it's globally accessible. So if : "12345" is accessed at another place. It should have the same address. I : am not 100% sure about this.
|
y**i 发帖数: 1112 | 9 你有没有尝试第三和第四种情况能不能修改字符元素呢?我觉得应该不能,尽管返回值
是char*
【在 r****o 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 刚才试了一下,两个const按存在不存在取4种组合,函数都能正确工作,除了一些 : warning以外。 : const char *function1(void) : { : const char *s = "12345"; : return s; : } : const char *function2(void) : { : char *s = "12345";
|