问题描述
为什么
i = ++ i;未定义
但
i = func(++ i);定义了吗?
我知道在评估参数后调用函数
后会有一个序列点。但我无法确定序列点如何定义第二个语句
定义的
?也有人可以给我一个详细的解释
为什么第一个陈述是未定义的?
Thanx提前任何帮助...
标准规定一个对象不应该有它的存储值
在序列点之间多次修改,你的例子修改
i两次没有干预SP,我认为非常直截了当。
如果你还是不明白,你必须更具体一点
抓什么麻烦。
Robert Gamble
我仍然不明白这样做有什么害处。可以
你请显示(一步一步)这将导致不同的结果
在不同的编译器上?
我仍然不明白这样做有什么害处。
伤害?它会调用未定义的行为。
你能否(请一步一步地)显示这将如何导致不同的编译器产生不同的结果?
除了标准所规定的上述限制之外,还有一个逻辑方法来评估表达式i = ++ i,同样的方式
即我= ++ j将被评估,在前一个例子中,i的值增加了
。但标准确实将限制
放在那里,因为我被修改了两次(一次用于增量运算符
和一次用于赋值运算符),行为是未定义的。我
我不知道关于
非歧义表达的限制背后的原因,但怀疑它与复杂性有关
涉及尝试提出措辞来定义什么是
模棱两可,什么不是。
Robert Gamble
Why
i = ++i; is undefined
but
i = func(++i); is defined ?
I know there is a sequence point after a call to a function
after the arguments have been evaluated. But I am not able
to visualise how the sequence point makes the second statement
defined ? Also can someone give me a detailed explanation of
why first statement is undefined ?
Thanx for any help in advance...
The standard states that an object shall not have its stored value
modified more than once between sequence points, your example modifies
i twice without an intervening SP, its pretty straight-forward I think.
If you still don''t understand you''ll have to be more specific about
what are having trouble grasping.
Robert Gamble
The standard states that an object shall not have its stored value
modified more than once between sequence points, your example modifies
i twice without an intervening SP, its pretty straight-forward I think.
If you still don''t understand you''ll have to be more specific about
what are having trouble grasping.
Robert Gamble
Still I don''t understand what''s the harm in doing that. Can
you please show (step by step) how this will lead to different results
on different complilers ?
Still I don''t understand what''s the harm in doing that.
The harm? It invokes undefined behavior.
Can you please show (step by step) how this will lead to different results
on different complilers ?
Outside of the aforementioned restriction placed by the Standard, there
is only one logical way to evaluate the expression i=++i, the same way
that i=++j would be evaluated, with the value of i being increased by
one in the former example. But the Standard does place the restriction
there and since i is modified twice (once for the increment operator
and once for the assignment operator) the behavior is undefined. I
don''t know exact the reasoning behind the restriction with regards to
non-ambiguous expressions but suspect it has to do with the complexity
involved with trying to come up with wording to define what is
ambiguous and what is not.
Robert Gamble
这篇关于为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!