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
On Jun 5, 11:39 pm, glen herrmannsfeldt <g...@ugcs.caltech.edu> 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.
Perhaps it does, but on any realistic platform, why would this matter if we're measuring the size of a short and a long? -- Oli
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. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
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.
It is true that sizeof() returns the size in chars. It is also true that in C, a char is, by definition, a byte. However, even if you take "byte" to mean "8 bits," my statement was not incorrect since Robert stated up front that the compiler was for the i386, and that machine has 8-bit bytes. I am aware of this "byte" definition since I ran into it on the TI TMS C54x C compiler, where sizeof(int) = 1, even though an int is 16 bits. That machine's native datapath is 16 bits wide and incapable of representing a type smaller than 16 bits. -- % Randy Yates % "She has an IQ of 1001, she has a jumpsuit %% Fuquay-Varina, NC % on, and she's also a telephone." %%% 919-577-9882 % %%%% <yates@ieee.org> % 'Yours Truly, 2095', *Time*, ELO http://home.earthlink.net/~yatescr
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? -- % 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
In article <m3odjuc93d.fsf@ieee.org>, Randy Yates  <yates@ieee.org> wrote:

>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?
For a common method of implementation, yes. If you printed two values the second would print as zero. But varargs implementations can be more complicated. They could use separate arrays for arguments of different types, in which case they would look in completely the wrong place for the argument. They could also pass some arguments in registers, in which case it might work regardless of endianness, or always fail. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
Richard Tobin said:

> 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?
The requirement is to be able to represent at least 256 discrete values. This can be accomplished in six trits, the relevant trit pattern being 100110. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
In article <582dnQ-CT9LncPjbnZ2dnUVZ8qaqnZ2d@bt.com>,
Richard Heathfield  <rjh@see.sig.invalid> 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?
>The requirement is to be able to represent at least 256 discrete values. >This can be accomplished in six trits, the relevant trit pattern being >100110.
What I was getting at was whether the definition of octet should be taken to be 8 bits, or 8 whatsits (where whatsit = bit, trit, etc). That is, can a six-trit word be considered to be smaller than an octet, defeating the claim that a byte may not be smaller than octet? -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
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"? -- glen
Vladimir Vassilevsky wrote:

(even more 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.
I would say it is the reader of the output that makes that assumption, not the program. If you write: printf("sizeof(short) = %d, sizeof(unsigned long) = %d\n", (int)sizeof(short), (int)sizeof(unsigned long)); then it is more obvious that it is returning the result of sizeof. -- glen
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.