Timeline for What are sequence points, and how do they relate to undefined behavior?
Current License: CC BY-SA 4.0
68 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 25, 2024 at 15:12 | history | edited | Prasoon Saurav | CC BY-SA 4.0 |
deleted 136 characters in body
|
| Jun 1, 2023 at 11:25 | history | edited | Prasoon Saurav | CC BY-SA 4.0 |
deleted 138 characters in body
|
| Apr 8, 2022 at 12:11 | history | edited | Aconcagua | CC BY-SA 4.0 |
Simple formatting adjustment not to leave a single question mark its own line...
|
| Jun 15, 2019 at 3:00 | history | edited | L. F. | CC BY-SA 4.0 |
use original spelling "behavior" in the standard quote instead of "behaviour"
|
| Sep 6, 2018 at 5:37 | history | edited | Gaurang Tandon | CC BY-SA 4.0 |
add slight info
|
| S Jan 5, 2018 at 4:31 | history | suggested | Community Bot | CC BY-SA 3.0 |
There was an easy misunderstandable quote
|
| Jan 4, 2018 at 23:51 | review | Suggested edits | |||
| S Jan 5, 2018 at 4:31 | |||||
| May 23, 2017 at 10:31 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Nov 28, 2016 at 2:02 | history | edited | tdao | CC BY-SA 3.0 |
Improve the text of UB/well defined examples
|
| Nov 7, 2016 at 9:59 | comment | added | bumfo |
@user168715 In C11, "The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. ", thus in *p++, * is sequenced after p++. I think in C++ it is almost the same :p
|
|
| Sep 29, 2016 at 16:17 | comment | added | Debashish | a = i+++j ? UB or well defined ? I think well defined. | |
| Sep 6, 2015 at 10:32 | comment | added | Nishant |
in ++i = 2 ,++ has got precedence over =. So for i++ will be executed theh i=2 will be executed. And the final value of i will be 2. Now I cannot see any other execution path for this expression. Hence it should not be unspecified or undefined. I do understand the in the expression i is being assigned twice in between two sequence points, hence it should have unspecified or undefined behavior. But logically I can think of only one execution path, i.e, the behavior is fixed.
|
|
| Aug 3, 2015 at 0:41 | comment | added | Don Larynx | "In short, undefined behaviour means [...] your girlfriend getting pregnant." Er, no, not quite. | |
| Jul 15, 2015 at 2:47 | history | edited | Jonathan Leffler | CC BY-SA 3.0 |
Clarify that this is for C++98 and C++03
|
| Jun 3, 2015 at 8:36 | comment | added | gsamaras | I find an expression difficult to understand, so I asked a question: stackoverflow.com/questions/30614396/what-does-i-i-i-1-1-do. Hope it helps. | |
| Jun 3, 2015 at 8:14 | history | edited | gsamaras | CC BY-SA 3.0 |
deleted 2 characters in body
|
| Jun 18, 2014 at 18:17 | comment | added | Apriori |
@Prasoon Saurav: a common paradigm is to write code like int a, b, c = 0; a = b = c; However b is read from to assigned to a after it is written to (taking the value of c) with no sequence point in between. This would seem to violate "2) Furthermore, the prior value shall be accessed only to determine the value to be stored." and your explanation: "This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification." I think assignment chaining is well defined and the quote from the standard correct, perhaps the explanation is wrong?
|
|
| Jun 13, 2014 at 16:33 | history | edited | Anne | CC BY-SA 3.0 |
punctuation nits
|
| Apr 10, 2014 at 7:56 | history | edited | sharptooth | CC BY-SA 3.0 |
edited body
|
| Feb 24, 2014 at 18:28 | history | edited | Paul Evans | CC BY-SA 3.0 |
made consistent use of italics for: *side effects*
|
| Jan 14, 2014 at 15:46 | comment | added | Zeks | This answer seems to conflict with Prasoon's answer on the point of i = ++i +1 . One gives us UB but other - that it is defined behaviour :) Explain pls | |
| Jan 12, 2014 at 7:13 | review | Suggested edits | |||
| Jan 12, 2014 at 7:16 | |||||
| Sep 20, 2013 at 21:33 | comment | added | Erik P. |
@Adrian Your first example, i = (++i, ++i, i), is valid - it consists of evaluating ++i (read/write); then there's a sequence point; evaluating ++i (r/w); sequence point; evaluating i (r); finally assigning the last result to i (w). Every write to i is isolated from others by a sequence point, and every read from i is also OK. In your second example, though, you get: evaluate ++i (r/w), sequence point, evaluate ++i (r/w), sequence point, evaluate i++ (r/w), assign result to i (w). Now there's two writes to i after the last sequence point: that violates §5/4 rule 1).
|
|
| Sep 10, 2013 at 3:01 | history | edited | Cubbi | CC BY-SA 3.0 |
undo previous edit: ++i is most certainly lvalue
|
| Sep 9, 2013 at 14:17 | history | edited | Rag | CC BY-SA 3.0 |
This isn't going to work simply because ++i isn't an l value. It has nothing to do with sequence points.
|
| Jul 2, 2013 at 4:17 | comment | added | Adrian |
Ok,so am I correct that i = (++i, ++i, i) and i = (++i, ++i, i++) would be valid?
|
|
| Jul 1, 2013 at 7:09 | comment | added | Kolyunya |
@Adrian The first expression invokes an UB because there is no sequence point between the last ++i and the assignement to i. The second expression does not invoke UB because expression i does not change the value of i. In the second example the i++ is followed by a sequence point (,) before the assignment operator is called.
|
|
| Jun 24, 2013 at 19:20 | comment | added | Adrian |
I thought I understood sequence points as it relates to the comma operator but it looks like I'm mistaken. i = (i,++i,++i); is invalid and i = (++i,i++,i) is valid confuses me. Can you expand please?
|
|
| Apr 4, 2013 at 6:25 | comment | added | Koushik Shetty |
@BhavikShah in i = i++ , i++ modifies i between sequence points and then again value is assigned to itself. butin i = i+1, i is accessed but only to read the Previous value and this value is added with 1 and then stored in ionly once its modified.
|
|
| Dec 25, 2012 at 10:02 | comment | added | Bhavik Shah | why i=i++ is undefined behaviour and i=i+1 is fine?? | |
| Dec 10, 2012 at 10:21 | vote | accept | Prasoon Saurav | ||
| Nov 20, 2012 at 21:15 | history | edited | Anirudh Ramanathan | CC BY-SA 3.0 |
added 3 characters in body
|
| Oct 19, 2011 at 18:31 | history | edited | Paul Manta | CC BY-SA 3.0 |
deleted 1 characters in body
|
| Jul 15, 2011 at 20:22 | comment | added | supercat | @Mr. Anubis: A statement like a=(expression) should be regarded as: t1=a; a=BOOM; t2=(expression); DEFUSE a; a=t2;" "Direct" references to a in the right hand side will be replaced with t1. Any attempt to access A via any other means between "a=BOOM" and "DEFUSE a" is an error which, as far as the standard is concerned, could blow up your computer or anything within fifty feet of it. I think it should be pretty clear that one should stand well away from your computer while executing your example. | |
| Jul 15, 2011 at 20:08 | comment | added | supercat | @Mr. Anubis: A sequence point would imply that all side-effects from the expression to the left must occur before any portion of the expression on the right is evaluated. Your expression contains no sequence points. A statement like "x=x+1;" would translate into "t1=x+1; x=t1;", and the read of t1 is guaranteed to happen after the write. If your expression were changed to use three different variables (and thus be legit code), it could be resequenced many different ways. Note also that it may be helpful to add a further expansion: | |
| Jul 15, 2011 at 20:01 | comment | added | supercat | @Mr. Anubis: In thinking about sequencing, I would suggest that you write out statements using lots of temporary variables that are written at most once each, and is only read if written, such that each subexpression copies a real variable to a temp, copies a temp to a real variable, or acts between two or more temps. For example, "a=b+c" becomes "t1=b; t2=c; t3=t1+t2; a=t3;". Your example, assuming your second "=" was supposed to be "==", would become "t1=x; x=t1+1; t2=x+1; x=t2; t3=(t1==t2); x=t3;". A read of a temp must follow a write, but other than that things may happen "in parallel". | |
| Jul 15, 2011 at 18:27 | comment | added | Mr.Anubis |
@supercat : The term "sequence point" refers to a local, rather than global, phenomenon . i got the gist but can you explain more about this , and also my last question please , thanks
|
|
| Jul 15, 2011 at 18:22 | comment | added | Mr.Anubis |
@Terminal : ok let's take a example y = x++ = ++x; , as far as i know = doesn't define any sequence point , but due to right to left associativity this x++ = ++x; will be executed first , now tell me where are sequence points only in this (last expression) statement , so that i can say x is modified twice btw two consecutive sequence points
|
|
| Jul 12, 2011 at 19:22 | comment | added | supercat | @Terminal: The term "sequence point" refers to a local, rather than global, phenomenon. For example, in the statement a=(foo1(),foo2())+(bar1(),bar2()), there is a sequence point implying that foo1() is executed before foo2(), and likewise bar1() before bar2(), but there is no sequence point between any of the foo() calls and any of the bar() calls. | |
| Jul 12, 2011 at 11:07 | comment | added | Terminal | @M3taSpl0it : The first sequence point is the second comma operator in the expression and the second sequence point is the semi-colon. The value of "i" is modified twice between these two sequence points. | |
| Jul 11, 2011 at 8:07 | comment | added | user72424 |
@Prasoon : why is that i = (i,++i,++i); // Undefined Behaviour because there's no sequence point between ++i(right most) and assignment to i (i gets modified more than once b/w two SP) -> what i don't understand is that in your comments you just said there is no seq point btw i and ++i since (because = has no order of evaluation), but in parenthesis you said i gets modified between two SP . so what's actually true , whether SP exists or not?,
|
|
| May 24, 2011 at 4:02 | history | edited | Prasoon Saurav | CC BY-SA 3.0 |
edited body
|
| May 24, 2011 at 3:52 | comment | added | aschepler | Recommendation: Near the part that mentions the built-in comma operator has a sequence point, point out that the commas used to separate arguments to a function or elements in a braced initializer do not count as "comma operator". | |
| May 24, 2011 at 3:39 | history | edited | Prasoon Saurav | CC BY-SA 3.0 |
deleted 24 characters in body; deleted 2 characters in body
|
| May 24, 2011 at 2:24 | comment | added | sashang | @Prasoon: There's a contradictory statement. Above example 1 it states 'legal expressions to those in which the accesses demonstrably precede the modification.' yet the comment in Example 1 contradicts this (unless the use of 'legal expression' above was only meant to be something that satisfies the parser and not also imply it was defined) | |
| Dec 28, 2010 at 14:47 | vote | accept | Prasoon Saurav | ||
| Dec 28, 2010 at 14:47 | |||||
| Nov 29, 2010 at 12:57 | vote | accept | Prasoon Saurav | ||
| Nov 30, 2010 at 14:43 | |||||
| Nov 22, 2010 at 17:35 | vote | accept | Prasoon Saurav | ||
| Nov 25, 2010 at 11:11 | |||||
| Nov 19, 2010 at 16:25 | vote | accept | Prasoon Saurav | ||
| Nov 19, 2010 at 16:25 | |||||
| Nov 16, 2010 at 2:51 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
added 426 characters in body
|
| Nov 15, 2010 at 12:43 | history | made wiki | Post Made Community Wiki by user1228 | ||
| Nov 15, 2010 at 11:14 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
deleted 6510 characters in body; added 38 characters in body
|
| Nov 15, 2010 at 10:35 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
deleted 17 characters in body
|
| Nov 15, 2010 at 6:30 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
added 6489 characters in body
|
| Nov 14, 2010 at 18:28 | comment | added | Inverse | I'm not sure quoting the standard is the best way to teach newbies | |
| Nov 14, 2010 at 16:00 | comment | added | Mike DeSimone | Well, then you could have a link to the ISO's relevant order page. Anyway, thinking about it, the phrase "elementary knowledge of C++ Standard" seems a bit of a contradiction in terms, since if you're reading the standard, you're past the elementary level. Maybe we could list what things in the language you need a basic understanding of, like expression syntax, order of operations, and maybe operator overloading? | |
| Nov 14, 2010 at 14:13 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
corrected minor typos
|
| Nov 14, 2010 at 9:19 | comment | added | sbi | @Mike: AFAIK, there are no (legal) copies of the C++ Standard you could link to. | |
| Nov 14, 2010 at 9:13 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
deleted 144 characters in body
|
| Nov 14, 2010 at 9:11 | comment | added | Prasoon Saurav |
@user168715 : Ok let me give you an example. For example i = i + i . Here we have 3 accesses of i. Here i is to be written within the same expression and all the accesses of i (One in LHS and 2 in RHS) are directly involved in computing the final value that has to be written. So it is fine. That means the prior values are accessed only to determine what has to be written.
|
|
| Nov 14, 2010 at 8:38 | comment | added | user168715 |
@Prasoon: So returning a copy of p does not count as "accessing" the "prior value" of p? Why not? Does "access" here have a technical meaning?
|
|
| Nov 14, 2010 at 7:32 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
added 1 characters in body
|
| Nov 14, 2010 at 6:46 | history | edited | Mike DeSimone | CC BY-SA 2.5 |
Clarified standard link and punctuation
|
| Nov 14, 2010 at 6:41 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
added 150 characters in body
|
| Nov 14, 2010 at 6:23 | comment | added | Prasoon Saurav |
*p++ = 4 isn't Undefined Behaviour . *p++ is interpreted as *(p++). p++ returns p(a copy) and the value in stored at the previous address. Why would that invoke UB? It is perfectly fine.
|
|
| Nov 14, 2010 at 6:06 | comment | added | user168715 |
I don't understand the "Furthermore" part. Doesn't that make postfix ++ useless? For example, in the expression *p++ = 4, the prior value of p is being accessed both to determine the value to be stored in p (OK), and to determine the address of where to store the 4 (not OK?). But surely this common idiom isn't undefined behavior?
|
|
| Nov 14, 2010 at 5:45 | history | edited | Prasoon Saurav | CC BY-SA 2.5 |
added 2 characters in body; deleted 2 characters in body
|
| Nov 14, 2010 at 5:39 | history | answered | Prasoon Saurav | CC BY-SA 2.5 |