Does this code exhibit undefined behaviour, because pbuf is modified
more than once between two adjacent sequence points?
My answer is no, because there's a sequence point after the evaluation
of all function arguments, and another after the copying of a returned
value and before the execution of any expressions outside the function
(1.9/17); and because the first modification of pbuf happens before the
former sequence point, and the second modification happens after the
latter sequence point.
Is my interpretation correct?
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
>
> char buf[N], *pbuf;
> // ...
> pbuf += sprintf(pbuf = buf, "Hello world");
>
> Does this code exhibit undefined behaviour, because pbuf is modified
> more than once between two adjacent sequence points?
>
> My answer is no, because there's a sequence point after the evaluation
> of all function arguments, and another after the copying of a returned
> value and before the execution of any expressions outside the function
> (1.9/17); and because the first modification of pbuf happens before the
> former sequence point, and the second modification happens after the
> latter sequence point.
>
> Is my interpretation correct?
>
I believe that you don't run afoul of "modified more than once", but that
you run afoul "the previous value shall only be accessed...". "a += b" is
equivalent to "a = a + b", except that a is only evaluated once (presumably
the evaluation is both an lvalue evaluation and rvalue evaluation - the
lvalue is used for the assignment, the rvalue for the addition).
The lvalue to rvalue conversion inherent in the rvalue evaluation doesn't
have to occur after evaluation of the right side of "a += b" (which would
make it not be the previous value). Its value doesn't depend on the
value of
"b". This lvalue to rvalue conversion in the left side hasn't a dependence
to the assignment in "pbuf = buf", therefor I think you violate this second
constraint, which will make you go undefined behavior.
--
> char buf[N], *pbuf;
> // ...
> pbuf += sprintf(pbuf = buf, "Hello world");
>
> Does this code exhibit undefined behaviour, because pbuf is modified
> more than once between two adjacent sequence points?
>
> My answer is no, because there's a sequence point after the evaluation
> of all function arguments, and another after the copying of a returned
> value and before the execution of any expressions outside the function
> (1.9/17); and because the first modification of pbuf happens before the
> former sequence point, and the second modification happens after the
> latter sequence point.
>
> Is my interpretation correct?
My answer is yes, but for another reason: operator+= consists of several
actions:
1 evaluate address of lhs
2 read old value of lhs
3 evaluate rhs
4 add results of steps 2 and 3
5 write back result of step 4 into address from step 1
Step 3 can be ordered anywhere in relation to steps 1 and 2, here I used
one of the problem orders. There are not two writes without a sequence
point, but a write may or may not happen between read and write belonging
together.
--
Greetings,
Jens Schmidt