Re: wrong code generated in gcc-4.1.1?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Hiroki,

> Is this a bug or am I violating C++ standard?

The code violates the C++ standard.

The used range of Quartet is -1 ... 2, which can be expressed in 3 signed
bits (0b111 is -1, 0b010 is 2), a signed bit-range of -4 (0b100) to 3
(0b011).

42 and 129 do not fit in this range.  Casting 42 and 129 into that range is
undefined.

The C++ compiler *KNOWS* that case FOURTY_TWO cannot be accessed, and is
dead code.  Why?  Because according to ISO 14882, Quartet *MUST* be between
-4 and 3 (3 signed bits).  And it *KNOWS* that int i was set from a Quartet.

The optimization that is performing that behavior is -ftree-dominator-opts,
which is enabled by -O1 and higher.

If you compile the code as C (via gcc) instead of C++ (via g++), you'll
notice that the same -ftree-dominator-opts does not optimize away the
FOURTY_TWO.  Why?  Because in ISO 9899, an enum is held in a signed int or
an unsigned int.

Also some nits...

C++ should use the C++ header file, #include <cstdio>, and not the C header
file #include <stdio.h>.

And the return type should be char const*, and not char*.

And, in my opinion, C++ code should avoid using C-style casts and instead
should use C++ verbose casts.

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux