> On Oct 13, 2015, at 15:50 , Greg Parker <gpar...@apple.com> wrote:
> 
> 
>> On Oct 13, 2015, at 3:24 PM, Graham Cox <graham....@bigpond.com> wrote:
>> 
>> In a piece of code I haven’t worked on in a while, I’m suddenly getting a 
>> new warning which I’ve never seen before:
>> 
>> “Multiple unsequenced modifications to ‘ix’”
>> 
>> Code is:
>> 
>> ix = ++ix % guess.count;
>> 
>> where ix is a NSUInteger.
>> 
>> Is this telling me that the order of the preincrement and the mod operation 
>> is undefined? Surely a preincrement is defined to happen first, that’s why 
>> it’s called a PREincrement? Or does the warning refer to something else? 
>> I’ve used this form of expression for years without any issues, why is it 
>> suddenly one?
> 
> The mod operator uses the value after the increment. That part is 
> well-defined.
> 
> The compiler is complaining about the two stores to `ix`, one from the 
> preincrement and one from operator =. Storing to the same location twice in 
> the same statement is undefined in many cases: the language does not specify 
> which one wins. Look up "sequence point" for more details (although the 
> current language standards no longer use the "sequence point" terminology).
> 
> I thought that this particular form was allowed, but I'm not enough of a 
> language lawyer to know for sure. You might get better answers from a 
> compiler mailing list.
> 
> You can avoid confusion (human and compiler) by writing the statement without 
> the preincrement's side effect:
>    ix = (ix+1) % guess.count;

Actually, yeah, this is a much better alternative.

The document I linked in my other answer 
(http://en.cppreference.com/w/c/language/eval_order), under "Rules," specifies 
where the sequence points are. As of C11, rule 10 applies: the value of the 
right-hand-side is sequenced before the assignment, but not the side effects of 
the right-hand side. So, it's possible that the assignment to ix could happen 
first, and then the increment. So, it would compute ix+1, use that value in 
evaluating the right hand side, assign that result to ix, then store the old 
ix+1 into ix.



-- 
Rick Mann
rm...@latencyzero.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to