Discussion:
Postfix increment operator?
(too old to reply)
Vincent Fatica
2010-05-31 15:30:07 UTC
Permalink
Will someone please explain why the third of these does not give the expected
result (which the first two do)? And, can I rely on the second behaving like
the first? Thanks.

y = a*x[n] + b*x[n+1] + c*x[n+2];

y = a*x[n] + b*x[n+=1] + c*x[n+=1];

y = a*x[n++] + b*x[n++] + c*x[n];
--
- Vince
Vincent Fatica
2010-05-31 15:36:59 UTC
Permalink
On 31 May 2010 11:30:07 -0400, Vincent Fatica <***@blackholespam.net> wrote:

|Will someone please explain why the third of these does not give the expected
|result (which the first two do)? And, can I rely on the second behaving like
|the first? Thanks.
|
|y = a*x[n] + b*x[n+1] + c*x[n+2];
|
|y = a*x[n] + b*x[n+=1] + c*x[n+=1];
|
|y = a*x[n++] + b*x[n++] + c*x[n];

Oops! Now I see that the second does *not* do what the first does ... still
like an explanation. Thanks.
--
- Vince
Bo Persson
2010-05-31 15:44:27 UTC
Permalink
Post by Vincent Fatica
Will someone please explain why the third of these does not give
the expected result (which the first two do)? And, can I rely on
the second behaving like the first? Thanks.
y = a*x[n] + b*x[n+1] + c*x[n+2];
y = a*x[n] + b*x[n+=1] + c*x[n+=1];
y = a*x[n++] + b*x[n++] + c*x[n];
This is undefined behavior, so there is no explanation. You cannot
update a variable twice between sequence points.

The second one doesn't really work either, but one possible effect of
undefined behavior is "seem to work".

Just go for

y = a*x[n] + b*x[n+1] + c*x[n+2];
n +=2;

and let the optimzer take care of that. The *compiler* is allowed to
transform it into the equivalent of the second or third option, even
if you are not.


Bo Persson
Vincent Fatica
2010-05-31 16:01:13 UTC
Permalink
On Mon, 31 May 2010 17:44:27 +0200, "Bo Persson" <***@gmb.dk> wrote:

|Just go for
|
|y = a*x[n] + b*x[n+1] + c*x[n+2];
|n +=2;
|
|and let the optimzer take care of that. The *compiler* is allowed to
|transform it into the equivalent of the second or third option, even
|if you are not.

Yes! The other two get optimized qite a bit (smart compiler!). When there are
actually four addends, the right way (above) uses 80 bytes more text.
Thanks.
--
- Vince
Igor Tandetnik
2010-05-31 15:42:29 UTC
Permalink
Post by Vincent Fatica
Will someone please explain why the third of these does not give the expected
result (which the first two do)? And, can I rely on the second behaving like
the first? Thanks.
y = a*x[n] + b*x[n+1] + c*x[n+2];
y = a*x[n] + b*x[n+=1] + c*x[n+=1];
y = a*x[n++] + b*x[n++] + c*x[n];
The last two statements both exhibit undefined behavior, by modifying the same object twice without an intervening sequence point. The second statement only behaves the way you expect by accident. The first statement is the only one here with well-defined, reliable semantics.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Ulrich Eckhardt
2010-06-01 07:03:54 UTC
Permalink
Post by Vincent Fatica
y = a*x[n] + b*x[n+1] + c*x[n+2];
y = a*x[n] + b*x[n+=1] + c*x[n+=1];
y = a*x[n++] + b*x[n++] + c*x[n];
For the record, and since nobody mentioned it: The order in which
expressions are evaluated is only governed by their dependencies, so for
example

f(g(), h());

might call g() first or h() first! This is just another point where your
code is not reliable.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Loading...