From: Vincent Fatica on
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
From: Vincent Fatica on
On 31 May 2010 11:30:07 -0400, Vincent Fatica <vince(a)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
From: Igor Tandetnik on
Vincent Fatica 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];

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
From: Bo Persson on
Vincent Fatica 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];

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


From: Vincent Fatica on
On Mon, 31 May 2010 17:44:27 +0200, "Bo Persson" <bop(a)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
 |  Next  |  Last
Pages: 1 2
Prev: "PORTING C" > 2 dim arrays?
Next: Why does this crash?