The compiler is right, and warns you about undefined behavior.
In the code lines above, the value of the same variable is
changed twice within the same statement:
var = ++var % MAX;
If var is initially 6, and MAX is 6:
- ++var will attempt to assign the value '6' to var
- the = operator will attempt to assign the value 0 to var
Which one occurs first is not defined, and the two
assignments may interfere in unexpected ways.

That's right. You modify rear and front twice between two sequence
points. That's undefined behaviour.
In these cases, the ++ operator increases rear (resp. front), and the =
operator assigns something to rear/front again. You don't actually mean
that. You don't care for the increase-object side-effect of ++; all you
need is the value-plus-one evaluation. So, instead, use an expression
that only gives you the _value_ rear+1, mod MAX.
> but executues alright.

By accident. UB is allowed to do what you think it will do. It's also
allowed to do this, _until_ you demonstrate the program to your
supervisor.

Yes you did. 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.''

rear = ++rear;

++, operates on an lvalue, increments the value in the object -- by one --
which rear represents
Now its stored value has been modified already, then you modify it again by
assigning
this value to the object which rear designates, which violates this sequence
point law
and thus the reason for the diagnostic which your compiler so kindly gave
you.

"Richard Bos" <> wrote in message
news:...
<snip> (working)
> By accident. UB is allowed to do what you think it will do. It's also
> allowed to do this, _until_ you demonstrate the program to your
> supervisor.

LOL. I think this is the best description of UB I've yet seen. Thanks.

"BruceS" <> wrote:
> "Richard Bos" <> wrote in message
> news:...
> <snip> (working)
> > By accident. UB is allowed to do what you think it will do. It's also
> > allowed to do this, _until_ you demonstrate the program to your
> > supervisor.
>
> LOL. I think this is the best description of UB I've yet seen. Thanks.

Not only that, but it's more or less realistic. Some forms of UB depend
on the system running the program - move it to your supervisor's
differently configured system to demonstrate it, and you might well find
that on this system, the effect of the UB is not to silently continue
but to crash or print garbage.
One particularly common form of this is the difference you get when you
always run your program in a debugging environment, but your supervisor
runs it directly from the OS.

"Richard Bos" <> wrote in message
news:...
> "BruceS" <> wrote:
>
> > "Richard Bos" <> wrote in message
> > news:...
> > <snip> (working)
> > > By accident. UB is allowed to do what you think it will do. It's also
> > > allowed to do this, _until_ you demonstrate the program to your
> > > supervisor.
> >
> > LOL. I think this is the best description of UB I've yet seen. Thanks.
>
> Not only that, but it's more or less realistic. Some forms of UB depend
> on the system running the program - move it to your supervisor's
> differently configured system to demonstrate it, and you might well find
> that on this system, the effect of the UB is not to silently continue
> but to crash or print garbage.
> One particularly common form of this is the difference you get when you
> always run your program in a debugging environment, but your supervisor
> runs it directly from the OS.

Exactly, and why I like it. Also, while some may laugh at the idea of nasal
demons, and keep using the code because "it works fine", they're more likely
to pay attention to a warning such as above. A cohort at my first C job
liked to use the phrase "working by accident", and demanded such things be
fixed, as we never knew what change might prevent the accident.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!