由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - what's wrong with this C++ code?
相关主题
why int** cannot convert to const int** ?数组指针的问题
谁来解释一下这个是compiler问题吗?const char *p, is it ok to change p[1] ?
在c中如果一个function return 一个字符串array和pointer在作为函数返回时有啥区别 (C)
问一个 char * 和 char [] 的问题difference between: char** p and char*p[] ??
输入参数的检查应该归caller还是callee?a simple C++ question
a question about CAST请教一个const pointer的问题
请版上的C++牛人讲一下g++的优化参数C++ formatted output question
C++ Q05: pointer to constant variable最新某公司onsite面试题 (转载)
相关话题的讨论汇总
话题: char话题: address话题: literal话题: pointer话题: c++
进入Programming版参与讨论
1 (共1页)
a*****n
发帖数: 1
1
char* getName()
{
char* name = "tony";

return name;
}
This function return a local pointer. However, "tony" is a string literal.
Isn't it in the global namespace?
Thanks.
t****t
发帖数: 6806
2
nothing wrong with the code.

【在 a*****n 的大作中提到】
: char* getName()
: {
: char* name = "tony";
:
: return name;
: }
: This function return a local pointer. However, "tony" is a string literal.
: Isn't it in the global namespace?
: Thanks.

a**a
发帖数: 416
3
真要挑刺,返回类型应该是const char *.

【在 a*****n 的大作中提到】
: char* getName()
: {
: char* name = "tony";
:
: return name;
: }
: This function return a local pointer. However, "tony" is a string literal.
: Isn't it in the global namespace?
: Thanks.

d******e
发帖数: 194
4
my guess:
"tony" is a literal, but also a constant. so compiler will allocate a buffer
for name and copy the litteral into it so that name is initialized. the
buffer is probably on stack and unwound after return. that means the
ruturned pointer will be invalid.

【在 a*****n 的大作中提到】
: char* getName()
: {
: char* name = "tony";
:
: return name;
: }
: This function return a local pointer. However, "tony" is a string literal.
: Isn't it in the global namespace?
: Thanks.

d******e
发帖数: 194
5
I played for a while with this code. it compiles without any complain.
However, the returned pointer, although as char*, can not be used to change
the string. A segment fault was generated when I do
char *p = getName();
p[0] = 'A';
check the address of each pointer: (a is a local var I added as a reference
of stack address)
address of a = 0xbfffd724
address of name = 0x84e71ab
address of p = 0x84e71ab
obviously, name refers to a global address, rather than on stack (I was
wrong).
So thrust, coul

【在 t****t 的大作中提到】
: nothing wrong with the code.
t****t
发帖数: 6806
6
as i said, nothing really wrong with the code. you may be picky and say it
should return const char*, but for compatibility concerns, c++ allows
assigning string literal (address) to char*, so compilers won't complain.
although this conversion is deprecated.

change
reference

【在 d******e 的大作中提到】
: I played for a while with this code. it compiles without any complain.
: However, the returned pointer, although as char*, can not be used to change
: the string. A segment fault was generated when I do
: char *p = getName();
: p[0] = 'A';
: check the address of each pointer: (a is a local var I added as a reference
: of stack address)
: address of a = 0xbfffd724
: address of name = 0x84e71ab
: address of p = 0x84e71ab

m***0
发帖数: 6
7
You can not modified a string literal, which is a const, eg "read only".
Nothing to do with pointers.

change
reference

【在 d******e 的大作中提到】
: I played for a while with this code. it compiles without any complain.
: However, the returned pointer, although as char*, can not be used to change
: the string. A segment fault was generated when I do
: char *p = getName();
: p[0] = 'A';
: check the address of each pointer: (a is a local var I added as a reference
: of stack address)
: address of a = 0xbfffd724
: address of name = 0x84e71ab
: address of p = 0x84e71ab

d******e
发帖数: 194
8
Is "read only" supposed to be only at compile time, not run-time?
"Can't change a litteral" is because a litteral is only a value at compile
time, and don't have an address in binary code. But here we do have an
address and a pointer, how, at instruction level, can a segment fault occur
when modifying a byte with a valid pointer?

【在 t****t 的大作中提到】
: as i said, nothing really wrong with the code. you may be picky and say it
: should return const char*, but for compatibility concerns, c++ allows
: assigning string literal (address) to char*, so compilers won't complain.
: although this conversion is deprecated.
:
: change
: reference

t****t
发帖数: 6806
9
usually these literals (constants) are in read-only memory, any write to
these memory will be catched by OS and cause seg fault

occur

【在 d******e 的大作中提到】
: Is "read only" supposed to be only at compile time, not run-time?
: "Can't change a litteral" is because a litteral is only a value at compile
: time, and don't have an address in binary code. But here we do have an
: address and a pointer, how, at instruction level, can a segment fault occur
: when modifying a byte with a valid pointer?

d******e
发帖数: 194
10
you mean these segments of memory are set to read-only in LDT?

【在 t****t 的大作中提到】
: usually these literals (constants) are in read-only memory, any write to
: these memory will be catched by OS and cause seg fault
:
: occur

m***0
发帖数: 6
11
The string literal is in text section (code) instead of data section (
variables), that is why it will cause segment fault if you modify it.

【在 d******e 的大作中提到】
: you mean these segments of memory are set to read-only in LDT?
1 (共1页)
进入Programming版参与讨论
相关主题
最新某公司onsite面试题 (转载)输入参数的检查应该归caller还是callee?
c++ 弱问题:static const char* const 这两个const 分别是什么意思?a question about CAST
为啥允许这样的const设计请版上的C++牛人讲一下g++的优化参数
问一个C++函数Parameter的问题C++ Q05: pointer to constant variable
why int** cannot convert to const int** ?数组指针的问题
谁来解释一下这个是compiler问题吗?const char *p, is it ok to change p[1] ?
在c中如果一个function return 一个字符串array和pointer在作为函数返回时有啥区别 (C)
问一个 char * 和 char [] 的问题difference between: char** p and char*p[] ??
相关话题的讨论汇总
话题: char话题: address话题: literal话题: pointer话题: c++