Multiple increment operators in single statement

Possible Duplicate: Undefined Behavior and Sequence Points Pleae explain the behaviour of following statements int b=3; cout<<b++*++b<<endl; How will it be calculated? The behavior here is undefined. See this question Relevant standard quote: §5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evalua

单个语句中的多个增量运算符

可能重复: 未定义的行为和序列点 请解释以下陈述的行为 int b=3; cout<<b++*++b<<endl; 它将如何计算? 这里的行为是未定义的。 看到这个问题 相关标准报价: §5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. 最常见的顺序点是声明的结尾。 从标准还值得注意的是: §5.2.2/8 The

Is this undefined behavior in C/C++ (Part 2)

This question already has an answer here: Why are these constructs (using ++) undefined behavior in C? 13 answers Undefined behavior and sequence points 4 answers It's undefined because of 2 reasons: The value of i is twice used without an intervening sequence point (the comma in argument lists is not the comma operator and does not introduce a sequence point). You're calling a

这是C / C ++中未定义的行为(第2部分)

这个问题在这里已经有了答案: 为什么这些构造(使用++)在C中的未定义行为? 13个答案 未定义的行为和序列点4个答案 由于以下两个原因而未定义: i的值在不插入序列点的情况下使用两次(参数列表中的逗号不是逗号运算符,也不引入序列点)。 你在范围内调用一个没有原型的可变参数函数。 传递给printf()的参数数量与格式字符串不兼容。 默认的输出流通常是行缓冲的。 如果没有'n' ,则不能保证输出将被

Why are multiple statements in ternary operator not executed

This question already has an answer here: Undefined behavior and sequence points 4 answers What's the precedence of comma operator inside conditional operator in C++? 3 answers Because of operators priority. Your expression evaluates as ((a) ? (nb++, nb2++) : nb--), nb2--; Operator , ( comma ) is the last thing to process. And this example would not compile at all but The express

为什么三元运算符中的多个语句没有执行

这个问题在这里已经有了答案: 未定义的行为和序列点4个答案 C ++中条件运算符中的逗号运算符的优先级是什么? 3个答案 由于运营商的优先。 你的表达评估为 ((a) ? (nb++, nb2++) : nb--), nb2--; 操作员, ( comma )是处理的最后一件事。 而这个例子根本不能编译 条件运算符中间的表达式(?和:)之间的表达式被解析为加括号。 有关详细信息,请参阅C ++运算符优先级。 这是预期的行为。 编译器将您的表达理

Unexpected order of evaluation (compiler bug?)

Possible Duplicate: Undefined Behavior and Sequence Points I'm not sure if this is a gcc bug or not, so I'll ask: unsigned int n = 0; std::cout << n++ << n << ++n; gcc gives the extremely strange result: "122" which AFAICT is impossible. Because << is left associative, it should be the same as: operator<<(operator<<(operator<<(st

意外的评估顺序(编译器错误?)

可能重复: 未定义的行为和序列点 我不确定这是不是一个海湾合作委员会的错误,所以我会问: unsigned int n = 0; std::cout << n++ << n << ++n; 海湾合作委员会给出了非常奇怪的结果:“122”AFAICT是不可能的。 因为<<是左联合的,所以它应该和下面的一样: operator<<(operator<<(operator<<(std::cout, n++), n), ++n) 并且因为在评估参数之前和之后都有一个序列点,所以

Twisted C++ code

Possible Duplicate: Undefined behavior and sequence points #include< iostream.h> int main() { int i=7,j=i; j=(i++,++i,j++*i); cout <<j; return 0; } What will be the output of the C++ code? It's my homework that my professor gave me. It sometimes helps to convince people who don't believe this is undefined by actually compiling the program

扭曲的C ++代码

可能重复:未定义的行为和顺序点 #include< iostream.h> int main() { int i=7,j=i; j=(i++,++i,j++*i); cout <<j; return 0; } 什么是C ++代码的输出? 这是我的教授给我的作业。 有时通过用几个编译器编译程序并观察结果,有助于说服那些不相信这个不确定的人: 修复iostream.h错误后, g ++ 4.5.2打印64 CLang ++ 2.8打印63 Sun C ++ 5.8打印63 MSVC 2010打印64

Sequence Points vs Operator Precedence

Possible Duplicate: Unsequenced value computations (aka sequence points) Undefined Behavior and Sequence Points Operator Precedence vs Order of Evaluation I'm still trying to wrap my head around how the following expression results in undefined behavior: a = a++; Upon searching SO about this, I found the following question: Difference between sequence points and operator precedenc

序列点与运算符优先级

可能重复: 非序列值计算(aka序列点) 未定义的行为和序列点 运营商优先级与评估顺序 我仍然试图围绕下面的表达式如何导致未定义的行为: a = a++; 在搜索这个关于这个,我发现了以下问题: 序列点和运算符优先级之间的区别? 0_o 我通读了所有答案,但我仍然在细节上遇到困难。 其中一个答案将我上面的代码示例的行为描述为模糊,就a如何修改而言。 例如,它可以归结为以下两种情况之一: a=(a+1);a++; a++;a

When does postincrement i++ get executed?

Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level, when does the postincrement++ operator get executed? The precedence table indicates that postfix++ operators are level 2: which means in int x = 0 ; int y = x++ + x++ ; // ans: y=0 The postfix ++'s execute first. However, it would seem that the logical operation of this line is the addition h

何时postincrement i ++得到执行?

可能重复: 未定义的行为和序列点 在机器代码级别的C ++中,postincrement ++运算符何时执行? 优先表指示后缀++运算符为2级:表示in int x = 0 ; int y = x++ + x++ ; // ans: y=0 后缀++的执行第一。 但是,似乎这条线的逻辑运算是加法首先发生(0 + 0),但是这是怎么发生的呢? 我想象的是: // Option 1: // Perform x++ 2 times. // Each time you do x++, you change the value of x.. // but you "return"

What made i = i++ + 1; legal in C++17?

Before you start yelling undefined behaviour, this is explicitly listed in N4659 (C++17) i = i++ + 1; // the value of i is incremented Yet in N3337 (C++11) i = i++ + 1; // the behavior is undefined What changed? From what I can gather, from [N4659 basic.exec] Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressio

是什么让我=我++ + 1; 在C ++ 17合法吗?

在你开始大吼未定义的行为之前,这在N4659(C ++ 17)中明确列出, i = i++ + 1; // the value of i is incremented 然而在N3337(C ++ 11) i = i++ + 1; // the behavior is undefined 什么改变了? 从我可以收集的信息中,[N4659 basic.exec] 除非另有说明,否则对个别操作符和个别表达式的操作数的评估是不确定的。 [...]运算符操作数的值计算在运算符结果的值计算之前排序。 如果对存储器位置

Is modifying the pointed value and the pointer at the same time UB

I know that C and C++ and different languages, but the following applies to both. TL/DR I know that i = i++; is UB, because i is modified twice in the expression and C and C++ forbids it. References : C99 6.5 : If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar

在同一时间修改指向的值和指针UB

我知道C和C ++和不同的语言,但以下内容适用于两者。 TL / DR 我知道i = i++; 是UB,因为我在表达式中修改了两次,C和C ++禁止它。 参考: C99 6.5: 如果对标量对象的副作用与相同标量对象的不同副作用或使用相同标量对象的值进行值计算相反,则行为未定义。 如果表达式的子表达式存在多个可允许的排序,则如果在任何排序中发生这种无序的副作用,则行为是不确定的 C ++ 11 - 1.9 15: 如果对标量对象的副作

Why is i = v[i++] undefined?

From the C++ (C++11) standard, §1.9.15 which discusses ordering of evaluation, is the following code example: void g(int i, int* v) { i = v[i++]; // the behavior is undefined } As noted in the code sample, the behavior is undefined. (Note: The answer to another question with the slightly different construct i + i++ , Why is a = i + i++ undefined and not unspecified behaviour, might apply

为什么我= v [i ++]未定义?

从C ++(C ++ 11)标准,讨论评估顺序的§1.9.15,是以下代码示例: void g(int i, int* v) { i = v[i++]; // the behavior is undefined } 如代码示例中所述,行为未定义。 (注意:对于另一个与i + i++结构略有不同的问题的答案,为什么a = i + i ++未定义,而不是未指定的行为,可能适用于此:答案基本上是由于历史原因未定义行为,然而,这个标准似乎暗示了这个未定义的一些理由 - 请参见下面的引用。此外,这个关联