F
fctk
hello,
i'm trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.
first of all some terms:
* object = variable, element of an array, location pointed by a pointer
* modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)
if i understood correctly, there are two rules (checked in that order):
1) each object in an expression can be modified no more than once;
2) if an object is modified, then all the times that object is accessed
in order to be read, the values that are read must be all used for
calculating the final value of the object.
now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):
*p++ = *q++
i can't understand the following sentence:
"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."
i explain you my reasoning. first of all that expression could be
rewritten as:
((*(p++)) = (*(q++)))
there are 4 objects: p, q, *(p++), *(q++).
p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.
p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same for
q. *(p++) is never read, so it also respects rule 2.
so, why that expression is undefined?
to be honest, it does not say it is "undefined", but it speaks about
being allowed or not. so: in which case this expression is not allowed?
i can't see when...
also, the compiler (gcc) does not complain as in:
i = i++ /* warning: operation on `i' may be undefined */
so, what's the problem?
i'm trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.
first of all some terms:
* object = variable, element of an array, location pointed by a pointer
* modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)
if i understood correctly, there are two rules (checked in that order):
1) each object in an expression can be modified no more than once;
2) if an object is modified, then all the times that object is accessed
in order to be read, the values that are read must be all used for
calculating the final value of the object.
now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):
*p++ = *q++
i can't understand the following sentence:
"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."
i explain you my reasoning. first of all that expression could be
rewritten as:
((*(p++)) = (*(q++)))
there are 4 objects: p, q, *(p++), *(q++).
p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.
p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same for
q. *(p++) is never read, so it also respects rule 2.
so, why that expression is undefined?
to be honest, it does not say it is "undefined", but it speaks about
being allowed or not. so: in which case this expression is not allowed?
i can't see when...
also, the compiler (gcc) does not complain as in:
i = i++ /* warning: operation on `i' may be undefined */
so, what's the problem?