here is a post i put out (using Google Groups) that got dropped by
google:
i am using gcc as so:
$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)
and have compiled a simple test program (FILE: hello.c):
//
// $ gcc -Wconversion -o hello hello.c
// $ hello
//
#include <stdio.h>
main()
{
unsigned long a_ulong = 0; // 32 bit
short a_short_array[128]; // 16 bit each
a_ulong = 1234567;
a_short_array[26] = a_ulong;
printf("%d, %hx, %x, %lx \n", sizeof(a_short_array),
a_short_array[26], a_short_array[26], a_ulong );
//
// printf output is:
//
// 256, d687, ffffd687, 12d687
//
}
and ran it as so:
$ gcc -Wconversion -o hello hello.c
$ hello
getting output:
256, d687, ffffd687, 12d687
now, i have confirmed that a short is 16 bits and an unsigned long is
32 bits. why does not this line of code:
a_short_array[26] = a_ulong;
generate a warning when i have the -Wconversion or -Wall flags set on
the gcc invocation line?
there is clearly a loss of bits (or a changing of value).
here is what the manual says about it:
from http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options
:
-Wconversion
Warn for implicit conversions that may alter a value. This
includes conversions between real and integer, like abs (x) when x is
double; conversions between signed and unsigned, like unsigned ui =
-1; and conversions to smaller types, like sqrtf (M_PI). Do not warn
for explicit casts like abs ((int) x) and ui = (unsigned) -1, or if
the value is not changed by the conversion like in abs (2.0). Warnings
about conversions between signed and unsigned integers can be disabled
by using -Wno-sign-conversion.
For C++, also warn for conversions between NULL and non-pointer
types; confusing overload resolution for user-defined conversions; and
conversions that will never use a type conversion operator:
conversions to void, the same type, a base class or a reference to
them. Warnings about conversions between signed and unsigned integers
are disabled by default in C++ unless -Wsign-conversion is explicitly
enabled.
is there some other compiler flag i need to hit? i don't get why this
doesn't generate a warning.
finally, please reply to both newsgroups as i don't hang around
comp.lang.c very much.
thank you,
r b-j
how can i generate warnings for implicit casts that lose bits?
Started by ●June 5, 2007
Reply by ●June 5, 20072007-06-05
robert bristow-johnson <rbj@audioimagination.com> writes:> [...] > now, i have confirmed that a short is 16 bits and an unsigned long is > 32 bits.How did you do that? Try this: printf("size of short = %d, size of ulong = %d\n", sizeof(short), sizeof(unsigned long)); My suspicion is that they are both 32 bits (4 chars) on your machine and that's why you're not getting a warning. Robert, I NEVER use these data types anymore since I discovered stdint.h defined in C99. I instead use explicit types like int16_t, uint32_t, etc. -- % Randy Yates % "Remember the good old 1980's, when %% Fuquay-Varina, NC % things were so uncomplicated?" %%% 919-577-9882 % 'Ticket To The Moon' %%%% <yates@ieee.org> % *Time*, Electric Light Orchestra http://home.earthlink.net/~yatescr
Reply by ●June 5, 20072007-06-05
Randy Yates <yates@ieee.org> writes:> robert bristow-johnson <rbj@audioimagination.com> writes: >> [...] >> now, i have confirmed that a short is 16 bits and an unsigned long is >> 32 bits. > > How did you do that? Try this: > > printf("size of short = %d, size of ulong = %d\n", sizeof(short), sizeof(unsigned long));Doh! Sorry! I just reread your code and saw your statement verified there. I have no freaking idea why the compiler doesn't burp. Not even with -Wall do I get a warning on this conversion. -- % Randy Yates % "She's sweet on Wagner-I think she'd die for Beethoven. %% Fuquay-Varina, NC % She love the way Puccini lays down a tune, and %%% 919-577-9882 % Verdi's always creepin' from her room." %%%% <yates@ieee.org> % "Rockaria", *A New World Record*, ELO http://home.earthlink.net/~yatescr
Reply by ●June 5, 20072007-06-05
Randy Yates wrote: (snip)> printf("size of short = %d, size of ulong = %d\n", sizeof(short), sizeof(unsigned long));This makes the assumption that sizeof returns an int, when it often returns something else. Maybe you should also test sizeof(sizeof(int))==sizeof(int) -- glen
Reply by ●June 5, 20072007-06-05
glen herrmannsfeldt wrote:> Randy Yates wrote: > > (snip) > >> printf("size of short = %d, size of ulong = %d\n", sizeof(short), >> sizeof(unsigned long)); > > > This makes the assumption that sizeof returns an int, when it > often returns something else. Maybe you should also test > sizeof(sizeof(int))==sizeof(int) >This also makes the assumption that sizeof() returns size in bytes, whereas sizeof returns the size in chars. Char may be bigger then one byte. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Reply by ●June 5, 20072007-06-05
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:> This makes the assumption that sizeof returns an int, when it > often returns something else.sizeof's result is never an int, although it can be an unsigned int. -- Ben Pfaff http://benpfaff.org
Reply by ●June 5, 20072007-06-05
Vladimir Vassilevsky <antispam_bogus@hotmail.com> writes:> This also makes the assumption that sizeof() returns size in bytes, > whereas sizeof returns the size in chars. Char may be bigger then one > byte.This statement reflects some confusion about C definitions. In C, a char is always one byte, in that sizeof(char) is always 1. However, the size of a byte is implementation-defined: it may be larger than one octet (though not smaller). -- "It wouldn't be a new C standard if it didn't give a new meaning to the word `static'." --Peter Seebach on C99
Reply by ●June 5, 20072007-06-05
Vladimir Vassilevsky <antispam_bogus@hotmail.com> writes:> glen herrmannsfeldt wrote: > >> Randy Yates wrote: >> (snip) >> >>> printf("size of short = %d, size of ulong = %d\n", sizeof(short), >>> sizeof(unsigned long)); >> This makes the assumption that sizeof returns an int, when it >> often returns something else. Maybe you should also test >> sizeof(sizeof(int))==sizeof(int) >> > > This also makes the assumption that sizeof() returns size in bytes, > whereas sizeof returns the size in chars. Char may be bigger then one > byte.No, a "byte" is by definition the size of a char. The term "byte" may have other meanings outside the context of C, but sizeof(char) is 1 by definition. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply by ●June 5, 20072007-06-05
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:> Randy Yates wrote: > (snip) > >> printf("size of short = %d, size of ulong = %d\n", sizeof(short), sizeof(unsigned long)); > > This makes the assumption that sizeof returns an int, when it > often returns something else. Maybe you should also test > sizeof(sizeof(int))==sizeof(int)No, just convert it before printing it: printf("size of short = %d, size of unsigned long = %d\n", (int)sizeof(short), (int)sizeof(unsigned long)); Or use "lu" and unsigned long if the result of sizeof might exceed INT_MAX. Or use "%zu" if your implementation supports it. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply by ●June 5, 20072007-06-05
In article <878xay6os3.fsf@blp.benpfaff.org>, Ben Pfaff <blp@cs.stanford.edu> wrote:>However, the size of a byte is implementation-defined: it may be >larger than one octet (though not smaller).How big is an octet on ternary machines? -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.






