gcc-3.4.4-20050211: maybe a danger behaviour
James E Wilson
wilson@specifixinc.com
Thu Mar 10 21:49:00 GMT 2005
Denis Zaitsev wrote:
> a) Formally, the code is correct. As p->what can never be < 0
> according to its type.
I am assuming this is C code. C and C++ have different rules for enums.
This isn't strictly true. The C standard does not say that enums are
unsigned. It only says that they are equivalent to an integral type
with a few restrictions, and an implementation can choose to make it
signed or unsigned when all enum values are non-negative. So depending
on the compiler, it could be possible for p->what to be negative.
> b) GCC _silently_ allows the {-1} initialization for that type, even
> with -Wall.
This is because there is no such thing as an enum type in C, enums are
just an alias for an integral type, and it is never wrong to store -1 in
an integral type, even if it is unsigned. (C++ on the other hand will
complain here because C++ has actual enum types distinct from integral
types.)
> Uncommenting the c= -1 member of enum, or explicit casting p->what to
> int solves the problem, of course. But maybe some warning would be
> appropriate in such a situation?
You will get a warning if you compile with -W.
tmp.c:22: warning: comparison of unsigned expression >= 0 is always true
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com
More information about the Gcc
mailing list