Erik de Castro Lopo <erikd@mega-nerd.com> writes:> robert bristow-johnson wrote: > >> // $ gcc -Wconversion -o hello hello.c > > When using gcc, I usually use -Wall -Wextra -Werror which turns > on all warnings and makes all warnings into errors. > > Not sure if this will help in your case.It doesn't on my machine. [yates@localhost tmp]$ gcc -v Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: ./configure Thread model: posix gcc version 4.1.2 -- % 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
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
Jerry Avins said:> Keith Thompson wrote:<snip>>> 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. > > Isn't a byte in C the larger of character, octet, or smallest > addressable storage element?Well, that isn't how it's defined! But yes, your rule looks correct to me. I think it's easier to think of it as 8+ bits wide. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
Reply by ●June 5, 20072007-06-05
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. it's close to an assignment of one type to another but less severe. for example, if sizeof(unsigned short)<sizeof(long) we know that no value is changed in this assignment: unsigned short a_ushort; long a_long; a_ushort = 4321; a_long = a_ushort; so no warning should be generated, no matter what bits are in a_ushort, there is no change of value. whereas (assuming sizeof(short)<sizeof(unsigned long)) this: short a_short; unsigned long a_ulong; a_short = -4321; a_ulong = a_short; should generate a warning because there are values in the range of the type (short) that are not in the type (unsigned long). so even if the number of bits in the word are increasing in the assignment, this should generate a -Wcondition warning (maybe it does, but it should also do it for the original example i brought). so i can see this warning as being functionally different from one of type checking or even if there is a bit reduction. i just wish it worked right.> > > 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.yeah, i should look for a gnu or gcc newgroup. just dunno where. r b-j
Reply by ●June 5, 20072007-06-05
On Jun 5, 8:13 pm, Keith Thompson <k...@mib.org> wrote:> robert bristow-johnson <r...@audioimagination.com> writes: > > [...]> 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. > > [...] > > Try gnu.gcc.help.thanks. didn't even know about the gnu hierarchy. r b-j
Reply by ●June 5, 20072007-06-05
Richard Heathfield wrote:> Jerry Avins said: > >> Keith Thompson wrote: > > <snip> > >>> 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. >> Isn't a byte in C the larger of character, octet, or smallest >> addressable storage element? > > Well, that isn't how it's defined! But yes, your rule looks correct to > me. I think it's easier to think of it as 8+ bits wide.Do you have a better "rule" than mine to tell just how many that '+' implies? Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Reply by ●June 5, 20072007-06-05
On Tue, 05 Jun 2007 20:33:32 -0400, Jerry Avins <jya@ieee.org> wrote in comp.dsp:> Richard Heathfield wrote: > > Jerry Avins said: > > > >> Keith Thompson wrote: > > > > <snip> > > > >>> 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. > >> Isn't a byte in C the larger of character, octet, or smallest > >> addressable storage element? > > > > Well, that isn't how it's defined! But yes, your rule looks correct to > > me. I think it's easier to think of it as 8+ bits wide. > > Do you have a better "rule" than mine to tell just how many that '+' > implies?Very simple. Either you or your program should look at or include the standard C header <limits.h>. There's a perfectly good macro named CHAR_BIT defined there. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Reply by ●June 5, 20072007-06-05
Jerry Avins <jya@ieee.org> writes:> Richard Heathfield wrote: >> Jerry Avins said: >> >>> Keith Thompson wrote: >> <snip> >> >>>> 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. >>> Isn't a byte in C the larger of character, octet, or smallest >>> addressable storage element? >> Well, that isn't how it's defined! But yes, your rule looks correct >> to me. I think it's easier to think of it as 8+ bits wide. > > Do you have a better "rule" than mine to tell just how many that '+' > implies?A byte is exactly the size of a character (an object of type char). A byte is at least 8 bits (i.e., >= 1 octet). A byte is an addressable unit of data storage (i.e., at least as big as the smallest addressible storage element). Systems for which the natural size of a character is less than 8 bits, or where a character is not addressable, are shown no mercy; they must adapt somehow in order to be conforming. For example, on Cray vector systems, a character is 8 bits, but the smallest physically addressable storage unit is 64 bits. The C compiler fakes 8-bit addressability by storing the byte offset in the high-order bits of a pointer. There's no hardware support for this; it's done in software (i.e., by the machine code generated by the compiler). (CHAR_BIT *could* have been set to 64 rather than 8, but that would have broken interoperation with other systems.) -- 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 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^^^^^ always. An int is signed, a size_t is unsigned. sizeof(type) is a size_t.> sizeof(sizeof(int))==sizeof(int)Why?
Reply by ●June 5, 20072007-06-05
glen herrmannsfeldt wrote:> 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"?It returns a size_t, which is always defined as an unsigned type. -- <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 5, 20072007-06-05
robert bristow-johnson wrote:> > grumble, grumble... >... snip ...> > 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.Try "gcc -W -Wall -ansi -pedantic" -- <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







