由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一个精华区里的题目
相关主题
请帮忙看段code,为什么过不了。问个《编程实践》(英文版)里面的问题
how to access a const char array in a function请教一个fb面试问题
C++ 面试题请教一个const的问题
leetcode上wild match菜鸟求救 请大家看看我的代码有没有问题
被thank you的fb电面面经问个题?
G家电面,这回肯定挂了。附面经。请问这样写程序错了吗?
问个c++的问题A challenging interview question: revert a string
再问个简单的C问题这题谁知道答案?
相关话题的讨论汇总
话题: char话题: 12345话题: function1话题: void话题: return
进入JobHunting版参与讨论
1 (共1页)
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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 问个问题,第二个函数里面的文字常量是不是也会在静态区里创建内存,然后再拷贝到
: 栈里的数组,还是根本就不存在这样一个文字常量?还有就是这个文字常量的作用域怎
: 样?

f*******r
发帖数: 1086
7
我觉得不是,应该是test的函数本身很简单,
stack空间没有被occupy,返回一个local的指针变量
应该是不safe的

【在 r****o 的大作中提到】
: 刚才试了一下,两个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 的大作中提到】
:
: 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 的大作中提到】
: 刚才试了一下,两个const按存在不存在取4种组合,函数都能正确工作,除了一些
: warning以外。
: const char *function1(void)
: {
: const char *s = "12345";
: return s;
: }
: const char *function2(void)
: {
: char *s = "12345";

1 (共1页)
进入JobHunting版参与讨论
相关主题
这题谁知道答案?被thank you的fb电面面经
分享一道最近碰到的很好的面试题。G家电面,这回肯定挂了。附面经。
请教一个字符串比较排序的问题 (转载)问个c++的问题
问两个题再问个简单的C问题
请帮忙看段code,为什么过不了。问个《编程实践》(英文版)里面的问题
how to access a const char array in a function请教一个fb面试问题
C++ 面试题请教一个const的问题
leetcode上wild match菜鸟求救 请大家看看我的代码有没有问题
相关话题的讨论汇总
话题: char话题: 12345话题: function1话题: void话题: return