DSPRelated.com
Forums
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →

how can i generate warnings for implicit casts that lose bits?

Started by robert bristow-johnson ●June 5, 2007
In article <c9GdnRrzH9jRb_jbnZ2dnUVZ_segnZ2d@comcast.com>,
glen herrmannsfeldt  <gah@ugcs.caltech.edu> wrote:

>As someone else mentioned, %zu has been added to solve the problem.
I'd never noticed that before. Nor had I noticed %jd and %td. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
grumble, grumble...

okay guys we can argue a little bit about the sizes of types, but
isn't it clear that when i run these lines of code:

 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 );

and get this for output:

256, d687, ffffd687, 12d687

that the bits in the hex digits "12" went bye-bye in the assignment
statement?  i just wanna know what flag to set (if any) that makes the
compiler tell me i might want to check the statement that could
potentially throw away those bits.  i would think, from the
description that -Wconversion or -Wall should do it, but it doesn't
and i was wondering if the hardcore C or gcc geeks might know the
magic invocation to generate such a warning.

i'm no linux or gnu freak (really a neophyte), i just remember in my
old codewarrior days that there was a nice little check box i could
hit to see such warnings.  killing such warnings is a useful
discipline to have to avoid some unforseen bugs that might also be
hard to find.  it's sorta like enforcing strict type checking.

r b-j

Randy Yates wrote:

(snip)

>>I don't think there is a simple reliable way to print a size_t in >>general, since it could in principle be bigger than unsigned long >>long, but in this case using (int)sizeof(short) would work.
As someone else mentioned, %zu has been added to solve the problem.
> That brings up an interesting question: Doesn't this behavior depend > on the machine's endianness?
> For example, consider the statement
> printf("sizeof(int) = %d", sizeof(int));
> and the case in which int is 16 bits and sizeof() returns 32 bits.
> Is it true that a little-endian machine will print this correctly, > while a big-endian machine will not?
It is reasonably likely that it will, though it is still wrong. Also, that will only work if you are just printing one of them. I consider this a disadvantage of little-endian, as it tends to hide bugs due to type mismatch until it is too late. Consider also the mistake of printing a 16 bit value with a format item expecting 32 bits. (or any function expecting a 32 bit value.) It might work. -- glen
richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> 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?
An "octet" is by definition 8 bits. If you don't have bits, you can't have octets. Of course a ternary machine can emulate bits; the answer to your question then depends on how the emulation is done. If you use ternary machines, it might be reasonable to refer to a collection of 8 trits as an "octet". That would conflict with normal usage, but then so do ternary machines. -- 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"
richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <1181081659.289870.144410@w5g2000hsg.googlegroups.com>, > Oli Charlesworth <catch@olifilth.co.uk> wrote: > >>> > 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. > >>Perhaps it does, but on any realistic platform, why would this matter >>if we're measuring the size of a short and a long? > > Because they're passed to a varargs function (printf), which will try > to access them as ints, so if they're in fact bigger than ints - > regardless of their value - things will go horribly wrong.
Correction: things *might* go horribly wrong. Worse, they might go horribly right. (I say "horribly" because it could result in a failure to detect the error until the code is ported to another platform and fails at the most embarrassing posible moment.) -- 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"
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:

> Ben Pfaff wrote: >> 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. > > So I should have said "always" instead of "often"?
Yes. -- "Am I missing something?" --Dan Pop
robert bristow-johnson wrote:

(snip)

> that the bits in the hex digits "12" went bye-bye in the assignment > statement? i just wanna know what flag to set (if any) that makes the > compiler tell me i might want to check the statement that could > potentially throw away those bits. i would think, from the > description that -Wconversion or -Wall should do it, but it doesn't > and i was wondering if the hardcore C or gcc geeks might know the > magic invocation to generate such a warning.
As far as I know, this is part of C. Note that Java requires a cast for all narrowing conversions. Maybe you should switch to Java instead. -- glen
Randy Yates <yates@ieee.org> writes:
> richard@cogsci.ed.ac.uk (Richard Tobin) writes: >> In article <1181081659.289870.144410@w5g2000hsg.googlegroups.com>, >> Oli Charlesworth <catch@olifilth.co.uk> wrote: >> >>>> > 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. >> >>>Perhaps it does, but on any realistic platform, why would this matter >>>if we're measuring the size of a short and a long? >> >> Because they're passed to a varargs function (printf), which will try >> to access them as ints, so if they're in fact bigger than ints - >> regardless of their value - things will go horribly wrong. >> >> I don't think there is a simple reliable way to print a size_t in >> general, since it could in principle be bigger than unsigned long >> long, but in this case using (int)sizeof(short) would work. > > That brings up an interesting question: Doesn't this behavior depend > on the machine's endianness? > > For example, consider the statement > > printf("sizeof(int) = %d", sizeof(int)); > > and the case in which int is 16 bits and sizeof() returns 32 bits. > > Is it true that a little-endian machine will print this correctly, > while a big-endian machine will not?
Maybe. As far as standard C is concerned, it's undefined behavior. That means that the standard says absolutely nothing about what will happen. It might blow up, it might print correct or incorrect results, and it might make demons fly out of your nose (not likely, but if it does you can't complain that it violates the standard). It's conceivable that 32-bit and 16-bit integers are passed as arguments in different registers; attempting to read one when you were promised the other might give you garbage, regardless of endianness. The solution is quite simple: don't do that. (I see that this discussion is cross-posted to comp.dsp and comp.lang.c. That's probably not a good idea.) -- 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"
Vladimir Vassilevsky wrote:
> glen herrmannsfeldt wrote: >
... snip ...
>> >> 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 it can't. Read the standard. Chars may be bigger than 8 bits, but the size of a char is the same as the size of a byte. C is peculiar that way. -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
robert bristow-johnson said:

> isn't it clear that when i run these lines of code: > > 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 ); > > and get this for output: > > 256, d687, ffffd687, 12d687 > > that the bits in the hex digits "12" went bye-bye in the assignment > statement?
Yes. the C Standard does not require implementations to produce a diagnostic message in this circumstance. A conversion is supplied. Of necessity, if the lvalue is less wide than the rvalue, any information stored in those extra bits will be lost. Nevertheless, the conversion is a useful one in situations where no information is lost, and to take advantage of it does not constitute a syntax error or constraint violation, so no diagnostic message is required.
> i just wanna know what flag to set (if any) that makes the > compiler tell me i might want to check the statement that could > potentially throw away those bits.
Check in a newsgroup that deals with your implementation. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.