Re: Simultaneous Writes on Volatile

On Aug 16, 2:07 pm, Shao Miller <> wrote:
It's valid in that it compiles,

The translation thus has defined behaviour.

Uh, no.


i = 0;
i = ++i - i--;

What is the current value of 'i' after the 2nd statement? Every C
compiler must accept this program, but they can do anything they want
with it. It's syntactically valid.

but not so in that it has defined

The run-time behaviour for this particular example is out-of-scope of
the Standard, right?

No. Actually the standard prescribes fairly exactly the side effects
of expressions (not down to the implementation details mind you).

arr[arr[0]-1] could be arr[0] meaning the other
arr[arr[0]] references don't have a compile time defined behaviour
[hence it's UB].

I don't follow you here.  What is it that's missing in order for
translation behaviour to be well-defined?  If you are suggesting that
the run-time behaviour is dependent on factors outside of the scope of
the Standard, then that's part of what I was asking about, for sure.
I think that's what you're suggesting, here.

What is missing here is the standard does not prescribe the order of
evaluation of the individual terms. Even in something like

j = ++i * i - i++;

You know that it's equivalent to (++i * i) - i++, but how the
individual terms are computed is up to the compiler, it could be
computed as

A = i++
B = i
C = ++i
j = B*C + A

or equally valid as

A = ++i
B = i++
C = i
j = C*A + B

Both which produce a different value.

Furthermore, even if it were valid [which I contend it's not] it's
horrible and should not in any form be encouraged.

I guess we would have to come to an agreement about "valid" first,
then proceed with agreements from there.

Well it's syntactically valid code, it will compile, but the standard
doesn't prescribe the BEHAVIOUR of the code.

One possible use is to attempt to determine characteristics about an
implementation which are not required to be documented.  A battery of
tests that exercise potentially awkward situations can possibly reveal
some statistics that could be used towards Bayesian inferences about
behaviour to rely upon.  There're no guarantees, so such tests might
give the next best thing, when an implementation's vendor is not
interested or no longer available.  Who will do this?  Maybe nobody.
Other uses are possible, but I don't think it matters.  We can invent
uses on-the-fly, so "horrible" is subject to change, or is a predicate
in certain contexts such as "in most cases".  That's fine by me.

Well you wouldn't have to guess what is UB or not UB if you read the
damn spec. It specifically denotes things like that [lacking a
sequence point] as causing UB.

Basically the rule is simple, if a single statement [more or less]
uses a variable [or through an array potentially the same variable]
multiple times with modification it's likely to be UB.

so just like i = ++i * i--; is likely to be a bad idea so is i =
arr[0]++ * --arr[0]; or i = arr[arr[0]]++ * --arr[arr[0]]; ...