Timeline for Why is x=x++ undefined?
Current License: CC BY-SA 3.0
30 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 25, 2017 at 13:43 | comment | added | user251748 | "Doctor, it hurts when I do this!" "Well, don't do that!" | |
| Jun 20, 2012 at 6:16 | comment | added | ugoren | @Secure, Your distinction is probably correct, but I don't think it matters much - this rule is a big source of misunderstanding. I do think the rule is hard to explain - the kind of thing a person understands when you explain, possibly remembers, and fails to connect with real code when it's relevant. | |
| Jun 19, 2012 at 17:42 | answer | added | Random832 | timeline score: 5 | |
| Jun 19, 2012 at 16:03 | answer | added | John Bode | timeline score: 5 | |
| Jun 19, 2012 at 15:44 | comment | added | Secure | "the rule forbidding changing a variable twice between sequence points is certainly a rule most programmers don't understand." -- Do you have any evidence for this? Most questions I've seen were asked because the programmers didn't know about the rule. Is there any evidence that most of them still didn't understand it after it was explained? | |
| Jun 19, 2012 at 14:51 | comment | added | AProgrammer | There are several questions and I'm confused about which ones you are really interested. Why the current state? What better state could be reasonably achieved as an evolution of C? What state would be desirable for C if one wasn't constrained? What state would be desirable for a "C like" language? If it is one of later two, I would tend to vote to close your question as "non constructive", i.e. far too open to get a definitive answer. | |
| Jun 19, 2012 at 14:34 | history | edited | ugoren | CC BY-SA 3.0 |
added 701 characters in body
|
| Jun 19, 2012 at 14:24 | comment | added | ugoren |
@MichaelKjörling, I'm not arguing about undefined behavior in general. I'm just trying to remove the rule that makes x=x++ undefined.
|
|
| Jun 19, 2012 at 14:04 | comment | added | user |
@JohnB As much as undefined behavior is undesireable in production code, it generally is not illegal according to the language spec. Dereferencing an unitialized variable as a pointer (something like int paddr; char *p = *paddr;) often is also undefined behavior.
|
|
| Jun 19, 2012 at 14:03 | comment | added | NoChance | @ugoren, in C#, int x=0; x = x + (x = 3); results in x holding 3 - No errors or warning in either cases. | |
| Jun 19, 2012 at 13:54 | history | edited | ugoren | CC BY-SA 3.0 |
added 348 characters in body
|
| Jun 19, 2012 at 12:31 | answer | added | DeadMG | timeline score: -2 | |
| Jun 19, 2012 at 12:27 | comment | added | JohnB | I would find a more interesting question to be why isn't an error to write this? Surely a compiler could detect it's undefined behavour and therefore can't possibly be what the user actually wanted, so why isn't in an error? I understand some instances of undefined bahavour are hard to detect but this isn;t | |
| Jun 19, 2012 at 12:09 | answer | added | Jerry Coffin | timeline score: 23 | |
| Jun 19, 2012 at 11:54 | answer | added | AProgrammer | timeline score: 2 | |
| Jun 19, 2012 at 10:34 | history | tweeted | twitter.com/#!/StackProgrammer/status/215029907988815872 | ||
| Jun 19, 2012 at 10:12 | comment | added | ugoren |
@EmmadKareem, is this guaranteed behavior in C#? Or is it unspecified, as in my x=x+(x=3) example?
|
|
| Jun 19, 2012 at 8:56 | comment | added | NoChance | Interesting question. Notice that in C#, x+=(x=3) results in 3 not 6 (assuming x was 0 before the statement). | |
| Jun 19, 2012 at 8:13 | comment | added | Loki Astari | @ugoren: Why do you need to predict the result. Nobody in their right mind would write code like that (as has been mentioned several times) even if you did write code like this it would be rejected at the first code review. So there is no need to define the behavior and give the optimizer the best chance at optimizing it. In every example you propose I would shoot somebody if they added that to the code base. | |
| Jun 19, 2012 at 8:07 | answer | added | Tanzelax | timeline score: 10 | |
| Jun 19, 2012 at 8:06 | comment | added | Jamie Taylor | In my mind, this is a good question ("Some men see things as they are and ask why, I dream things that never were and ask why not"). It's (in my opinion) a question purely on language design, using C syntax as an example, not a question on C syntax. I, personally, think that the reason we don't have defined behaviour for expressions such as x++ + ++x or x=x++ is simply because there is a possibility of them being misread. | |
| Jun 19, 2012 at 7:59 | history | edited | ugoren | CC BY-SA 3.0 |
added 712 characters in body
|
| Jun 19, 2012 at 7:52 | comment | added | ugoren | @Caleb, I think the current rules are very unclear and confusing. Half the C questions in stackoverflow.com are about this. I think my suggestions make it much easier to predict an expression's result. | |
| Jun 19, 2012 at 7:51 | comment | added | Joachim Sauer | Case in point: Java (and I believe C# as well) do have defined behaviour for those statements. It's still a bad idea to use them, because they are still confusing to humans. | |
| Jun 19, 2012 at 7:49 | answer | added | Secure | timeline score: 25 | |
| Jun 19, 2012 at 7:43 | comment | added | Caleb |
Why is this important to you? Should it be defined, and if so, why? There's not much point in assigning x to itself, and if you want to increment x you can just say x++; -- no need for the assignment. I'd say it should not be defined just because it'd be hard to remember what's supposed to happen.
|
|
| Jun 19, 2012 at 7:43 | answer | added | mouviciel | timeline score: 2 | |
| Jun 19, 2012 at 7:37 | comment | added | ugoren |
@honk, I don't see why it makes optimizations impossible. For example, x=x++, which doesn't change x, can be optimized to nothing.
|
|
| Jun 19, 2012 at 7:33 | comment | added | Benjamin Bannier |
You point 2. makes even the simplest optimizations impossible, I wouldn't want to trade that in just so I can x=x++. Maybe you could make a point why these new rules would be better than what currently almost every C-like language does.
|
|
| Jun 19, 2012 at 7:24 | history | asked | ugoren | CC BY-SA 3.0 |