z***e 发帖数: 5393 | 1 void change(const char* s) //s="abcdefg"
{
char* a=NULL;
a=(char*)s;
*a='*';
printf("%c\n",*a); //第一次输出*
printf("%c\n",*a); //第二次输出a
}
int main()
{
const char *s="abcdefg";
change(s);
}
为什么第一次输出*?vc下运行。 |
t****t 发帖数: 6806 | 2 You are not supposed to do that (change char literal). And if you do? Yes
you guessed it, it's undefined. So compiler has no problem, you have.
【在 z***e 的大作中提到】 : void change(const char* s) //s="abcdefg" : { : char* a=NULL; : a=(char*)s; : *a='*'; : printf("%c\n",*a); //第一次输出* : printf("%c\n",*a); //第二次输出a : } : int main() : {
|
c*****g 发帖数: 119 | 3 more accurately, changing the values in a char array literal is undefined.
【在 t****t 的大作中提到】 : You are not supposed to do that (change char literal). And if you do? Yes : you guessed it, it's undefined. So compiler has no problem, you have.
|
b******g 发帖数: 54 | 4 那为什么我在VC++上运行都成功,而且输出都是* *?
【在 c*****g 的大作中提到】 : more accurately, changing the values in a char array literal is undefined.
|
z***e 发帖数: 5393 | 5 I know, I know...
I just want to find out what the "undefined" actually do.
【在 t****t 的大作中提到】 : You are not supposed to do that (change char literal). And if you do? Yes : you guessed it, it's undefined. So compiler has no problem, you have.
|
c*****g 发帖数: 119 | 6 most times, it leads to a run-time error.
【在 z***e 的大作中提到】 : I know, I know... : I just want to find out what the "undefined" actually do.
|
c*****g 发帖数: 119 | 7 undefined, it depends on the implementation of compilers.
【在 b******g 的大作中提到】 : 那为什么我在VC++上运行都成功,而且输出都是* *?
|
j****g 发帖数: 597 | 8 I think zlike's question is even it's undefined in the standard, it should
be consistent in a specific compiler. May in linux u get * but in vc u get a
. But for the same compiler, it should output the same. |
t****t 发帖数: 6806 | 9 why do you think so?
if it's undefined, it's undefined. anything could happen.
it's a dangerous tempt, to try to find out what will actually happen in "
undefined" situation, unless of course if you are writing compilers. you may
get wrong or misleading image, which may cause trouble in later programming
. if you only remember "it's undefined", then you won't touch it anymore.
a
【在 j****g 的大作中提到】 : I think zlike's question is even it's undefined in the standard, it should : be consistent in a specific compiler. May in linux u get * but in vc u get a : . But for the same compiler, it should output the same.
|
j****g 发帖数: 597 | 10 打个比方。没在linux试过。
不过就算crash也是consistent的呀。 |
d*******d 发帖数: 2050 | |
d*******d 发帖数: 2050 | 12 我在linux下试验过相似的情况,crash.
【在 j****g 的大作中提到】 : 打个比方。没在linux试过。 : 不过就算crash也是consistent的呀。
|
z***e 发帖数: 5393 | 13 err....I was just doing it for fun.
and I gave up. different compiler does give different result, even for vc
itself. And release/debug build gives different runtime result...
My thought is because of the optimization of control flow. The output of *a=
'*' is used in the next instruction printf("%c",*a), so the printf parameter
is linked to '*' instead of *a. Since this is a function call which might
have side effect, the 2nd printf then needs to fetch the real data from *a.
Just my guess.
may
【在 t****t 的大作中提到】 : why do you think so? : if it's undefined, it's undefined. anything could happen. : it's a dangerous tempt, to try to find out what will actually happen in " : undefined" situation, unless of course if you are writing compilers. you may : get wrong or misleading image, which may cause trouble in later programming : . if you only remember "it's undefined", then you won't touch it anymore. : : a
|