C
cmelliso
I'm working on a formal definition for C and I'd like to clarify some
information about expression evaluation. Please accept my apologies
if these questions have been asked before; I looked around but
couldn't find these particular answers.
First: why the change to the description of expression evaluation in
Sec 6.5:2 from ISO/IEC 9899:1999 to 9899:201x? It used to say:
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be read only to determine the value
to be stored."
Now it says:
"If a side effect on a scalar object is unsequenced relative to either
a different side effect on the same scalar object or a value
computation using the value of the same scalar object, the behavior is
undefined. If there are multiple allowable orderings of the
subexpressions of an expression, the behavior is undefined if such an
unsequenced side effect occurs in any of the orderings."
What is the difference? It seems like the older definition was more
clear. Furthermore, it seems like they both imply the following
simple statement:
"Between the previous and next sequence point you can't write to a
location twice or read from a location after writing to it. For a
given expression, if there is any evaluation order where such a thing
could happen, the expression is undefined."
Also, the introduction of "indeterminate sequencing" for functions
just confuses things in my mind, as it seems like nothing changed.
There were already sequence points at function calls and returns, so
it seems clear that related sub-expressions at the call-site would
have been sequenced before or after the call. What am I missing?
Finally, if my simplification is correct, I don't see why one needs to
consider delayed side effects, at least in establishing
undefinedness. If there is a problem in an expression involving a
side effect, the path on which you detect that problem would be the
one where the side effect happens as early as possible. If there is
no problem, then there is no way to detect that there was delayed side-
effecting anyway. It seems to me the only way you can "detect"
delayed side-effects is by writing an expression that is undefined
anyway.
As an example,
((x=y) + ...) + ...
The y itself must be evaluated before you can determine what the value
of the assignment expression is. Thus, there's no way to have the
assignment take place BEFORE that evaluation. Further, if you delay
changing the object x represents in memory, the only way you could
detect that in an expression would be by reading or writing x, which
would cause the entire expression to be undefined. Again, what am I
missing?
Is this an oversimplification? I realize there are reams of documents
about sequence points and evaluation order (including JTC1/SC22/WG14
N925 and N926), but I'd like have a simple interpretation as long as
it's still correct.
I would prefer an example/counterexample if you think my
simplification misses something. That is probably the best way to
make me realize I'm dumb
Any information is welcome,
-Chucky
information about expression evaluation. Please accept my apologies
if these questions have been asked before; I looked around but
couldn't find these particular answers.
First: why the change to the description of expression evaluation in
Sec 6.5:2 from ISO/IEC 9899:1999 to 9899:201x? It used to say:
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be read only to determine the value
to be stored."
Now it says:
"If a side effect on a scalar object is unsequenced relative to either
a different side effect on the same scalar object or a value
computation using the value of the same scalar object, the behavior is
undefined. If there are multiple allowable orderings of the
subexpressions of an expression, the behavior is undefined if such an
unsequenced side effect occurs in any of the orderings."
What is the difference? It seems like the older definition was more
clear. Furthermore, it seems like they both imply the following
simple statement:
"Between the previous and next sequence point you can't write to a
location twice or read from a location after writing to it. For a
given expression, if there is any evaluation order where such a thing
could happen, the expression is undefined."
Also, the introduction of "indeterminate sequencing" for functions
just confuses things in my mind, as it seems like nothing changed.
There were already sequence points at function calls and returns, so
it seems clear that related sub-expressions at the call-site would
have been sequenced before or after the call. What am I missing?
Finally, if my simplification is correct, I don't see why one needs to
consider delayed side effects, at least in establishing
undefinedness. If there is a problem in an expression involving a
side effect, the path on which you detect that problem would be the
one where the side effect happens as early as possible. If there is
no problem, then there is no way to detect that there was delayed side-
effecting anyway. It seems to me the only way you can "detect"
delayed side-effects is by writing an expression that is undefined
anyway.
As an example,
((x=y) + ...) + ...
The y itself must be evaluated before you can determine what the value
of the assignment expression is. Thus, there's no way to have the
assignment take place BEFORE that evaluation. Further, if you delay
changing the object x represents in memory, the only way you could
detect that in an expression would be by reading or writing x, which
would cause the entire expression to be undefined. Again, what am I
missing?
Is this an oversimplification? I realize there are reams of documents
about sequence points and evaluation order (including JTC1/SC22/WG14
N925 and N926), but I'd like have a simple interpretation as long as
it's still correct.
I would prefer an example/counterexample if you think my
simplification misses something. That is probably the best way to
make me realize I'm dumb
Any information is welcome,
-Chucky