Redian新闻
>
what's wrong with this C++ code?
avatar
what's wrong with this C++ code?# Programming - 葵花宝典
a*n
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.
avatar
t*t
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.

avatar
a*a
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.

avatar
d*e
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.

avatar
d*e
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.
avatar
t*t
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

avatar
m*0
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

avatar
d*e
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

avatar
t*t
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?

avatar
d*e
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

avatar
m*0
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?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。