sequence-points

Is indexing a new map element and having something that reads it assigned to it undefined behaviour, or just unspecified?

心已入冬 提交于 2019-11-27 19:25:50
问题 After answering this question, there was a long discussion over whether the code in question was undefined behaviour or not. Here's the code: std::map<string, size_t> word_count; word_count["a"] = word_count.count("a") == 0 ? 1 : 2; First of all, it was well-established that this was at least unspecified. The result differs based on which side of the assignment is evaluated first. In my answer, I followed through each of the four resulting cases, with factors of which side is evaluated first

Which compilation flags should I use to avoid run time errors

和自甴很熟 提交于 2019-11-27 16:33:03
问题 Just learned here that -Wsequence-point comiplation flag will pop a warning when the code can invoke UB. I tried it on a statement like int x = 1; int y = x+ ++x; and it worked very nicely. Until now I have compiled with gcc or g++ only using -ansi -pedantic -Wall . Do you have any other helpful flags to make the code more safe and robust? 回答1: As alk summed up, use these flags: -pedantic -Wall -Wextra -Wconversion First, I think you don't want to use the -ansi flag, as suggested in Should I

Any good reason why assignment operator isn't a sequence point?

拟墨画扇 提交于 2019-11-27 14:20:58
Is there any good reason for operator = not being a sequence point? Both in C and C++. I have trouble thinking about an counter-example. By request: In general, things need a reason to be a sequence point. They don't need a reason not to be a sequence point; that's the default. For example, && must be a sequence point because of short-circuiting behaviour: if the left-hand side is false, the right-hand side must not be evaluated . (This is not just about optimization; the right-hand side could have side effects, and/or depend on the left-hand side being true, as in ptr && ptr->data .)

Is the comma in a variable list a sequence point?

你。 提交于 2019-11-27 14:20:18
In the following type of code is there a sequence point between each variable construction, or is the result undefined? int a = 0; int b = a++, c = a++; I wasn't able to find in the standard a specific reference to a sequence point here. Does that mean it is undefined, or just that I failed in my search? The completion of an expression is a sequence point, but does the above initialization also count? I believe behavior is well-defined because of 8[dcl.decl]/3 Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself. Which is even additionally

Is this code well-defined?

对着背影说爱祢 提交于 2019-11-27 13:30:49
This code is taken from a discussion going on here . someInstance.Fun(++k).Gun(10).Sun(k).Tun(); Is this code well-defined? Is ++k in Fun() evaluated before k in Sun()? What if k is user-defined type, not built-in type? And in what ways the above function calls order is different from this: eat(++k);drink(10);sleep(k); As far as I know, in both situations, there exists a sequence point after each function call . If so, then why can't the first case is also well-defined like the second one? Section 1.9.17 of the C++ ISO standard says this about sequence points and function evaluation: When

Why is a = (a+b) - (b=a) a bad choice for swapping two integers?

时间秒杀一切 提交于 2019-11-27 09:57:32
问题 I stumbled into this code for swapping two integers without using a temporary variable or the use of bitwise operators. int main(){ int a=2,b=3; printf("a=%d,b=%d",a,b); a=(a+b)-(b=a); printf("\na=%d,b=%d",a,b); return 0; } But I think this code has undefined behavior in the swap statement a = (a+b) - (b=a); as it does not contain any sequence points to determine the order of evaluation. My question is: Is this an acceptable solution to swap two integers? 回答1: No. This is not acceptable. This

Are there sequence points in the expression a^=b^=a^=b, or is it undefined?

泄露秘密 提交于 2019-11-27 07:45:11
问题 The allegedly "clever" (but actually inefficient) way of swapping two integer variables, instead of using temporary storage, often involves this line: int a = 10; int b = 42; a ^= b ^= a ^= b; /*Here*/ printf("a=%d, b=%d\n", a, b); But I'm wondering, compound assignment operators like ^= are not sequence points, are they? Does this mean it's actually undefined behavior? 回答1: a ^= b ^= a ^= b; /*Here*/ It is undefined behavior. You are modifying an object ( a ) more than once between two

Sequence Points between printf function args; does the sequence point between conversions matter?

最后都变了- 提交于 2019-11-27 05:56:11
问题 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 points and side effects in C

浪尽此生 提交于 2019-11-27 05:28:47
In this C-FAQ it is give about sequence point ; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. In the examples i = i++; a[i] = i++; it is clear from first sentence of the statement that these examples are results in undefined behavior . In explaining second sentence of the statement it is said that; second sentence says: if an object is written to within a full expression, any and all

Post Increment with respect to Sequence Points

女生的网名这么多〃 提交于 2019-11-27 04:57:15
问题 When does the post increment operator affect the increment? I have come across two opinions: 1) From http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm: POST means do the operation after any assignment operation. 2) Closer home, an answer on SO(albeit on C++) says: ... that delays the increment until the end of the expression (next sequence point). So does the post increment operation... A) wait until a sequence point is reached or B) happen post an assignment operator or C)