[Coco] Found strangeness in C compiler (bug)
Mathew Boytim
maboytim at yahoo.com
Tue May 21 08:12:52 EDT 2019
I agree the result is undefined. This is what gcc has to say
main.c:7:4: warning: operation on ‘da’ may be undefined [-Wsequence-point] *da++ = *dN++ * v - *da; // this is correct as the increment should happen after the assignment ~~^~
The standard doesn't say when the pointer is incremented, it only says where the result will be stored so you can't say "the increment will happen after the assignment". I have to agree that the behavior is not what I would expect but undefined is undefined. Bottom line is that you can't do that and expect it to be predictable.
Matt
On Tuesday, May 21, 2019, 2:06:14 AM EDT, Mark McDougall <msmcdoug at iinet.net.au> wrote:
On 21/05/2019 3:32 pm, Walter Zambotti wrote:
> *da = *dN++ * v - *da++; // this is wrong as the expression on the right
> increments da before it is assigns the value to where da points.
I think you'll find this behaviour is undefined by the standard, which
is why you have inconsistent results depending on type.
It's not something I'd code that way in any case. I think your "work
around" is better (more readable/maintainable) code.
Regards,
---8<------8<------8<------8<------8<------8<------8<------8<------8<------8<---
2) Between the previous and next sequence point, the prior value of a
scalar object that is modified by the evaluation of the expression, must
be accessed only to determine the value to be stored. If it is accessed
in any other way, the behavior is undefined.
cout << i << i++; // undefined behavior (until C++17)
a[i] = i++; // undefined behavior (until C++17)
--
Mark McDougall
<http://retroports.blogspot.com.au>
--
Coco mailing list
Coco at maltedmedia.com
https://pairlist5.pair.net/mailman/listinfo/coco
More information about the Coco
mailing list