(comp.lang.c removed) Randy Yates wrote: (snip)> Wouldn't a "ternary digit" be a "tit"??? "How many tits are in that byte?"...I always thought is was trit, but it could be tit. quits for base 4. -- glen
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 ●June 5, 2007
Reply by ●June 5, 20072007-06-05
Reply by ●June 5, 20072007-06-05
Keith Thompson wrote: (snip)> 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.Ternary machines are rare, but machines that can do decimal arithmetic aren't so rare. Even x86 can do it, though not all that easily. Soon there will be machines that can do floating point decimal arithmetic, as far as I know C allows that. -- glen
Reply by ●June 6, 20072007-06-06
robert bristow-johnson <rbj@audioimagination.com> writes:> On Jun 5, 8:06 pm, Richard Heathfield <r...@see.sig.invalid> wrote: >> 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. > > it just seems to me that this conversion qualifies as one that changes > value. then, according to the gcc doc, it should generate a - > Wconversion warning.From the manual: -Wconversion Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype. This includes conversions of fixed point to floating and vice versa, and conversions changing the width or signedness of a fixed point argument except when the same as the default promotion. Also, warn if a negative integer constant expression is implicitly converted to an unsigned type. For example, warn about the assignment x = -1 if x is unsigned. But do not warn about explicit casts like (unsigned) -1. What do they mean by "prototype"? No matter what the documentation says or the standards say, I also find this situation extremely frustrating and counter-productive. It seems like the compiler emits warnings (or errors) all the time on type conversions of little consequence, and yet when it comes to something that causes a real loss of information, it remains silent. This behaviour ought to be changed. -- % Randy Yates % "And all that I can do %% Fuquay-Varina, NC % is say I'm sorry, %%% 919-577-9882 % that's the way it goes..." %%%% <yates@ieee.org> % Getting To The Point', *Balance of Power*, ELO http://home.earthlink.net/~yatescr
Reply by ●June 6, 20072007-06-06
Jerry Avins wrote: (snip)> Isn't a byte in C the larger of character, octet, or smallest > addressable storage element?There are complications on some word addressable machines. For the PDP-10 it is suggested that CHAR_BIT be either 9 or 18. While its addressable unit is the 36 bit word, there are instructions to directly address halfwords, and it isn't so hard to address 9 bit bytes. There are stories of C compilers for the PDP-10, though they are not easy to find. -- glen
Reply by ●June 6, 20072007-06-06
CBFalconer wrote:> Vladimir Vassilevsky wrote:(snip)>>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.It depends on context. If one wrote a C program to print out the available disk space on a system, the user might expect it to be in eight bit bytes, even when CHAR_BIT was not 8. But yes, sizeof (which is not a function) units are sizeof(char), and are called bytes independent of the actual size. -- glen
Reply by ●June 6, 20072007-06-06
On Tue, 05 Jun 2007 15:05:00 -0700, Ben Pfaff <blp@cs.stanford.edu> wrote:>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).The C standard screwed up when it chose to use the term "byte" to refer to what is really a storage unit. Everyone who owns a hard drive knows that a byte is 8 bits. So do most programmers, regardless of their programming language choice. The Ada standard got it right, because it chose to use the term "storage unit" to refer to what the C standard refers to as a "byte". Ada83 pre-dates C90 by enough years--go figure. -- jay
Reply by ●June 6, 20072007-06-06
jaysome wrote:>... snip ...> > The C standard screwed up when it chose to use the term "byte" to > refer to what is really a storage unit. Everyone who owns a hard > drive knows that a byte is 8 bits. So do most programmers, > regardless of their programming language choice. > > The Ada standard got it right, because it chose to use the term > "storage unit" to refer to what the C standard refers to as a > "byte". Ada83 pre-dates C90 by enough years--go figure.C is screwed up in many ways, however people are used to it. You can't change it, so adapt. -- <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
Reply by ●June 6, 20072007-06-06
jaysome <jaysome@hotmail.com> writes:> On Tue, 05 Jun 2007 15:05:00 -0700, Ben Pfaff <blp@cs.stanford.edu> > wrote: > >>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). > > The C standard screwed up when it chose to use the term "byte" to > refer to what is really a storage unit. Everyone who owns a hard drive > knows that a byte is 8 bits. So do most programmers, regardless of > their programming language choice.The term "byte" was in widespread use for a number of years before the meanings other than 8 bits became rare. -- 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 6, 20072007-06-06
Richard Tobin wrote, On 05/06/07 23:14:> 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?Irrelevant. Integer types (including char) are required to use a pure binary representation :-) -- Flash Gordon
Reply by ●June 6, 20072007-06-06
robert bristow-johnson wrote:> 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 >Do not know about gcc, but lcc-win32 produces: Warning twarn.c: 14 Assignment of unsigned long to short. Possible loss of precision. You have to use a higher warning level: lcc -A twarn.c In general, lcc-win32 warns when sizes differ in an assignment, the recipient being smaller than the source.







