gcc warnings on truncations?
Is there a way to get gcc to warn (or ideally, error) on truncations to shorter types? ie:
int len = strlen(blah);
should warn because strlen returns a size_t, which on 64-bit platforms is 8 bits and ints are 4. This would be a nice sanity check in our porting effort to make sure there's no possibility of loosing data. I've poked through the docs and nothing seemed to do it.
Ted Logan
Spatial Technology
Is there a way to get gcc to warn (or ideally, error) on truncations to shorter types? ie:
No.
The ISO C standard says that all expressions undergo default promotions, and default promotions require promoting char/short to int. Thus even a simple expression like
short s; short t; s = s + t;
results in truncation to a shorter type, because the result of s+t is an int and this result must be truncated to fit in the short destination. A naive implementation of this warning would give so many false warnings that it wouldn't be very useful. An intelligent implementation could cut down on the false warnings, but would take much longer to write, and hence it is unlikely that anyone will bother.
Jim