Author |
Message |
David Rubi #1 / 13
|
 still UB?
I keep asking about this, but it confuses me to no end. I know that i = ++i % 2; yields UB. What about i = (++i) % 2; Thanks for your patience. david -- If 91 were prime, it would be a counterexample to your conjecture. -- Bruce Wheeler
|
Tue, 16 Mar 2004 23:40:13 GMT |
|
 |
Tobias Oe #2 / 13
|
 still UB?
Quote:
> I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; > yields UB. What about > i = (++i) % 2; > Thanks for your patience. > david
int main(void){ int i=0; i = ++i % 2; i = (++i) % 2; Quote: }
gamow:~/C>ccc -std1 main.c cc: Warning: main.c, line 4: In this statement, the expression "i=++i%2" modifies the variable "i" more than once without an intervening sequence point. This behavior is undefined. (undefvarmod) i = ++i % 2; --------^ cc: Warning: main.c, line 5: In this statement, the expression "i=(++i)%2" modifies the variable "i" more than once without an intervening sequence point. This behavior is undefined. (undefvarmod) i = (++i) % 2; --------^ Tobias.
|
Wed, 17 Mar 2004 00:04:22 GMT |
|
 |
Ben Pfaf #3 / 13
|
 still UB?
Quote:
> I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; > yields UB. What about > i = (++i) % 2;
That's exactly the same thing. Parentheses do not add a sequence point. In both statements, `i' is modified twice between sequence points.
|
Wed, 17 Mar 2004 00:03:10 GMT |
|
 |
David Nea #4 / 13
|
 still UB?
On Fri, 28 Sep 2001 11:40:13 -0400, David Rubin said: Quote: >I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; >yields UB. What about > i = (++i) % 2;
An understanding of sequence points helps enormously with things like this. Section 3 of the FAQ (http://www.eskimo.com/~scs/C-faq/) goes into great depth on this. A sequence point is one of ",", ";", "&&", "||", between "?:", and just before a function call (that one I'm not too clear on myself). At a sequence point, everything before (left of) the sequence point is guaranteed to be evaluated, so it's side-effects are known after the sequence point. So i++, i++; or i++ && i++; are OK, and i is 2 more at the end than it was at the start. It is not allowed to assign to the same variable twice between sequence points, because there's no way of telling which happens first. So, for example, j = (i++) * (i++); is bad, because i gets changed twice between sequence points (the end of the last expression, and the semi-colon at the end). Likewise, reading & modifying a variable between sequence points is bad, because it's impossible to tell whether the read occurs before or after the change in value (so i=7 && j=(i++)+i; could have j=14 or 15, depending on whether the second i was evaluated before or after the ++ was done on the first i). The general rule, which will never give you these problems if you follow it, is 1) If you reference a variable in an expression, don't change it in the same expression, and 2) if you change a variable in an expression, don't reference it in the same expression, except as part of the calculation for the new value (eg. i = i+1). See question 3.8 for a better description. Dave. --
Palamon Technologies Ltd. Phone +353-1-634-5059
|
Wed, 17 Mar 2004 00:10:17 GMT |
|
 |
Clark S. Cox I #5 / 13
|
 still UB?
Quote:
> I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; > yields UB. What about > i = (++i) % 2;
Still just as undefined. Maybe you want one of the following instead: ++i; i %= 2; i %= 2; ++i; i = (i+1) % 2; -- Clark S. Cox III
http://www.whereismyhead.com/clark/
|
Wed, 17 Mar 2004 02:19:56 GMT |
|
 |
Kaz Kylhe #6 / 13
|
 still UB?
Quote:
>I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; >yields UB. What about > i = (++i) % 2;
This makes no difference at all. The expression is already parsed as (i = ((++i) % 2)) so the parentheses you added are superfluous. Note that the undefinedness is not related to operation order, but to evaluation order. The order in which operands are evaluated is unrelated to the order in which the arithmetic operations are carried out, except to the extent that all the input values to an operation must be established before it can be carried out.
|
Wed, 17 Mar 2004 02:29:06 GMT |
|
 |
Dan P #7 / 13
|
 still UB?
Quote: >I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; >yields UB. What about > i = (++i) % 2;
You forgot to explain us why you think the extra parentheses are supposed to make any difference. Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Thu, 18 Mar 2004 06:31:47 GMT |
|
 |
Mark McIntyr #8 / 13
|
 still UB?
Quote:
>>I keep asking about this, but it confuses me to no end. I know that >> i = ++i % 2; >>yields UB. What about >> i = (++i) % 2; >You forgot to explain us why you think the extra parentheses are supposed >to make any difference.
No, Dan, that was his question. "do they make any difference?". . Of course, you knew that, so I'm confused about why you didn't just answer, "the extra parens make no difference, parens are not sequence points here". -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Thu, 18 Mar 2004 07:05:37 GMT |
|
 |
ralmi #9 / 13
|
 still UB?
Quote:
> No, Dan, that was his question. "do they make any difference?". . > Of course, you knew that, so I'm confused about why you didn't just > answer, "the extra parens make no difference, parens are not sequence > points here".
Why *doesn't* C make parens sequence points? Ralmin.
|
Thu, 18 Mar 2004 07:59:31 GMT |
|
 |
Ben Pfaf #10 / 13
|
 still UB?
Quote:
> > No, Dan, that was his question. "do they make any difference?". . > > Of course, you knew that, so I'm confused about why you didn't just > > answer, "the extra parens make no difference, parens are not sequence > > points here". > Why *doesn't* C make parens sequence points?
For the same reason that C doesn't make addition and subtraction and multiplication into sequence points: it could potentially obstruct the compiler's ability to optimize. If you need to force order of operations, use separate statements.
|
Thu, 18 Mar 2004 08:14:52 GMT |
|
 |
Kaz Kylhe #11 / 13
|
 still UB?
Quote:
>> No, Dan, that was his question. "do they make any difference?". . >> Of course, you knew that, so I'm confused about why you didn't just >> answer, "the extra parens make no difference, parens are not sequence >> points here". >Why *doesn't* C make parens sequence points?
Because then (((a + b) + c) + d) woud have a different meaning from a + b + c + d.
|
Thu, 18 Mar 2004 08:38:42 GMT |
|
 |
k.. #12 / 13
|
 still UB?
Quote: > I keep asking about this, but it confuses me to no end. I know that > i = ++i % 2; > yields UB. What about > i = (++i) % 2;
Since it's the same code (apart from redundant parentheses), it also has UB (not necessarily the *same* UB, of course). -- Chris "electric hedgehog" Dollin C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
|
Fri, 19 Mar 2004 20:12:52 GMT |
|
 |
Dan P #13 / 13
|
 still UB?
Quote:
>>>I keep asking about this, but it confuses me to no end. I know that >>> i = ++i % 2; >>>yields UB. What about >>> i = (++i) % 2; >>You forgot to explain us why you think the extra parentheses are supposed >>to make any difference. >No, Dan, that was his question. "do they make any difference?". .
Try to think. Yes, I know I'm asking you the impossible :-) If he asked the question, it is because he thought the parentheses might change something. If he didn't think that, his question wouldn't have made any sense. Hence my question. Quote: >Of course, you knew that, so I'm confused about why you didn't just
Since your usual state of mind is being confused, you don't even have to mention it :-) Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Sat, 20 Mar 2004 04:46:32 GMT |
|
|
|