由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 谁来解释一下这个是compiler问题吗?
相关主题
问一个C++函数Parameter的问题C array
what's wrong with this C++ code?A helloworld OpenMP question?
question for C++ constantA C++ compiler related interview question
C++ 的 问题a[i]=i++
数组问题问一个简单C++问题
怎么得到char *分配空间的大小?问个字符串的基本问题
问个指针array 的简单问题strcat()
请问c++为什么会编译失败?[合集] 简单问题一个:为什么可以间接访问私有指针成员
相关话题的讨论汇总
话题: undefined话题: char话题: compiler话题: printf话题: 输出
进入Programming版参与讨论
1 (共1页)
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
11
en, i think so.
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

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 简单问题一个:为什么可以间接访问私有指针成员数组问题
C puzzle 一日一题怎么得到char *分配空间的大小?
请教C++里面这个表达问个指针array 的简单问题
问一道C++的题目。 (转载) 请问c++为什么会编译失败?
问一个C++函数Parameter的问题C array
what's wrong with this C++ code?A helloworld OpenMP question?
question for C++ constantA C++ compiler related interview question
C++ 的 问题a[i]=i++
相关话题的讨论汇总
话题: undefined话题: char话题: compiler话题: printf话题: 输出