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?
|