GCC Non-bugs
gcc.gnu.org
gcc.gnu.org
> This is often caused by a violation of aliasing rules, which are part of the ISO C standard. These rules say that a program is invalid if you try to access a variable through a pointer of an incompatible type.
Regarding this, is this what reinterpret_cast was at least partially designed for?
reinterpret_cast is meant to be really unsafe, to meant that in this type of casts the compiler will blindly do it, whereas the other *_cast there is still some type validation involved.
Reason I ask and not test is that it seems that VC++ does not take advantage of this undefined behavior and the sample code always works 'as expected'.
Actually this is the big difference between standard defined languages and those that rely on a single implementation.
There is nothing to rely upon if it isn't defined in the standard as such.
Perhaps what you intend with that statement is that issues like that of 1/3 are a cause for these sorts of issues, but interestingly (at least in JS), 1/3 is terminating, and results in an "expected" value.
The statement that "1/3 is terminating" is not true in binary, it is not true in decimal, and the only reason that you sometimes get the expected results is that sometimes the rounding errors will cancel out.
You can't dodge all your rounding problems by changing base. There are binary-coded decimal (BCD) systems that store numbers as strings of decimal digits. You can generally find these in calculators (ever notice that a TI-84 overflows after 9.(9)e99) or some financial software. However, you'll still have problems like the 0.1 * 0.2 issue in binary floating-point. For example (assuming shorter-than-usual numbers):
2/3 = 0.66... ~ 0.66667
2/3 + 2/3 ~ 0.66667 + 0.66667 = 1.33334
However, 4/3 = 1.33333... ~ 1.33333, which isn't the same result.
Basically, you can't get away from this problem, you can only push it around to cases you care less about.
(You probably can't find an API for binary-coded decimal in $LANGUAGE unless $LANGUAGE is often used for tasks where you really really don't want any float-related gotchas since most everyone else doesn't care too much).
And only converting them to floating point for display purposes: http://www.mpfr.org/mpfr-current/mpfr.html#index-mpfr_005fse...
The problem is that decimal isn't supported by modern hardware so it's not supported natively by C which adds very little to hardware representations.
From Wikipedia's entry on decimal64[1]: "It is intended for applications where it is necessary to emulate decimal rounding exactly, such as financial and tax computations."
[1] http://en.wikipedia.org/wiki/Decimal64_floating-point_format
static char * s = * &"hello";
This should not compile, clang refuses it, and it is not part of the C standard. GCC accepts it, but is it worth even submitting a bug report?
static char s[] = * &"foo";
The c standard says char array initializers are either a string, or a string surrounded by optional '{}' .
This document says this is a GCC-specific extension. Is this true? Or is it one of those things that's not standardized, but the compiler vendors all do it anyway?
[0] see "ISO/IEC 9899:1999 Technical Corrigendum 3", http://www.open-std.org/Jtc1/sc22/wg14/www/docs/n1235.pdf
Type punning by union is implementation defined behavior. Everyone fortunately defines it to Do The Right Thing (TM).
The truly standard supported way to type pun is by memcpy
float f = ...; int x; memcpy(&x, &f, sizeof x);
char bytes[5] = {0};
float flt = *(float*)&(bytes[1]);
Using memcpy works on any platform with 4 byte floats: char bytes[5] = {0};
float flt;
memcpy(&flt, &(bytes[1]), 4);Embedded targets are always memory-constrained. If they aren't, you are wasting money on hardware.
See this paper as to why it was removed from the spec: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n142...
This makes me wonder why they couldn't have implemented this rearrangement as export...