sequence-points

Sequence point from function call?

寵の児 提交于 2019-12-01 04:37:15
This is yet another sequence-point question, but a rather simple one: #include <stdio.h> void f(int p, int) { printf("p: %d\n", p); } int g(int* p) { *p = 42; return 0; } int main() { int p = 0; f(p, g(&p)); return 0; } Is this undefined behaviour? Or does the call to g(&p) act as a sequence point? No. It doesn't invoke undefined behavior. It is just unspecified , as the order in which the function arguments are evaluated is unspecified in the Standard. So the output could be 0 or 42 depending on the evaluation order decided by your compiler. The behavior of the program is unspecified since we

Why is this Undefined Behavior?

前提是你 提交于 2019-12-01 04:19:32
问题 Why does the following given expression invoke undefined behavior? int i = 5; i = (i,i++,i) + 1 My question is influenced by Als' question here 回答1: It isn't undefined. Answered here for C, Sequence points and partial order I think the same applies in C++ (and here's my response before I saw that link): The comma operator introduces a sequence point (and constrains to some extent the order in which the expression must be evaluated - left before right), so: the two modifications of i are

What's wrong with this fix for double checked locking?

岁酱吖の 提交于 2019-11-30 20:57:50
So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class singleton { private: singleton(); // private constructor so users must call instance() static boost::mutex _init_mutex; public: static singleton & instance() { static singleton* instance; if(!instance) { boost::mutex::scoped_lock lock(_init_mutex); if(!instance) instance = new singleton; } return *instance; } }; The problem apparently is the line

Is this program having any sequence point issues?

爱⌒轻易说出口 提交于 2019-11-30 20:54:33
问题 #include<stdio.h> int main() { int i=7,j; j=(i++,++i,j*i); return 0; } j=(i++,++i,j*i);Is this well defined ? Let me clear my doubt. 回答1: This expression is OK because the comma operator is a sequence point: (i++, ++i, j*i) However do not confuse it with the following where the comma is not acting as a sequence point: somefunction(i++, ++i, j*i) What about j = i++ * ++i The multiplication operator is not a sequence point. ( Excuse me hijacking your answer ) From §3.4 of ISO 9899:1999 (C

Sequence Points in printf

与世无争的帅哥 提交于 2019-11-30 18:43:26
I read here that there is a sequence point: After the action associated with input/output conversion format specifier. For example, in the expression printf("foo %n %d", &a, 42) , there is a sequence point after the %n is evaluated before printing 42 . However, when I run this code : int your_function(int a, int b) { return a - b; } int main(void) { int i = 10; printf("%d - %d - %d\n", i, your_function(++i, ++i), i); } Instead of what I expect I get: 12 - 0 - 12 Meaning that there was not a sequence point created for the conversion format specifier. Is http://en.wikipedia.org wrong, or have I

sequence points in java

早过忘川 提交于 2019-11-30 17:38:01
问题 Is there a guaranteed sequence of execution of the following java code: int i = getA() + getB(); Is getA() always executed before getB() , as any average person would expect? 回答1: Yes, it is. From the JLS, section 15.7: The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right. It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression

Undefined behavior, or: Does Swift have sequence points?

无人久伴 提交于 2019-11-30 14:13:59
In C/C++, the second statement in int i = 0; int j = i++ + i++ + ++i; invokes both unspecified behavior , because the order of evaluation of operands is unspecified, and undefined behavior , because the side effects on the same object i are unsequenced relative to each other. See for example Why are these constructs (using ++) undefined behavior? Undefined behavior and sequence points Now, given that Swift was designed as a safe language, what is the corresponding situation here? Is the result of var i = 0 let j = i++ + i++ + ++i well-defined? Can one conclude from the language reference in

Sequence points and side effects: Quiet change in C11?

旧城冷巷雨未停 提交于 2019-11-30 13:48:40
问题 C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 72) Furthermore, the prior value shall be read only to determine the value to be stored. 73) with the footnotes 72) A

Sequence points and side effects: Quiet change in C11?

孤人 提交于 2019-11-30 08:35:17
C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 72) Furthermore, the prior value shall be read only to determine the value to be stored. 73) with the footnotes 72) A floating-point status flag is not an object and can be set more than once within an expression. 73)

Undefined behavior, or: Does Swift have sequence points?

雨燕双飞 提交于 2019-11-29 20:21:19
问题 In C/C++, the second statement in int i = 0; int j = i++ + i++ + ++i; invokes both unspecified behavior , because the order of evaluation of operands is unspecified, and undefined behavior , because the side effects on the same object i are unsequenced relative to each other. See for example Why are these constructs (using ++) undefined behavior? Undefined behavior and sequence points Now, given that Swift was designed as a safe language, what is the corresponding situation here? Is the