Redian新闻
>
谁来解释一下这个是compiler问题吗?
avatar
谁来解释一下这个是compiler问题吗?# Programming - 葵花宝典
z*e
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下运行。
avatar
t*t
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()
: {

avatar
c*g
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.

avatar
b*g
4
那为什么我在VC++上运行都成功,而且输出都是* *?

【在 c*****g 的大作中提到】
: more accurately, changing the values in a char array literal is undefined.
avatar
z*e
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.

avatar
c*g
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.

avatar
c*g
7
undefined, it depends on the implementation of compilers.

【在 b******g 的大作中提到】
: 那为什么我在VC++上运行都成功,而且输出都是* *?
avatar
j*g
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.
avatar
t*t
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.

avatar
j*g
10
打个比方。没在linux试过。
不过就算crash也是consistent的呀。
avatar
d*d
11
en, i think so.
avatar
d*d
12
我在linux下试验过相似的情况,crash.

【在 j****g 的大作中提到】
: 打个比方。没在linux试过。
: 不过就算crash也是consistent的呀。

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

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