Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Function factories |
| Date | 2012-08-23 19:01 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <5036B657.20707@verizon.net> (permalink) |
| References | (8 earlier) <k162la$vqr$7@solani.org> <503690A7.6030404@verizon.net> <k1642l$1rd$2@news.albasani.net> <ln8vd5gtts.fsf@nuthaus.mib.org> <k169mp$1rd$4@news.albasani.net> |
On 08/23/2012 06:09 PM, Melzzzzz wrote: > On Thu, 23 Aug 2012 14:51:59 -0700 > Keith Thompson <kst-u@mib.org> wrote: > >> Melzzzzz <mel@zzzzz.com> writes: >> [...] >>> Well compiler warnings are enough aren't they? Cast just masks >>> warning and makes compiler happy which is not good thing. >>> Well if you have to search trough code to find that one error is >>> much more difficult rather then just check compiler warning. >>> I don;t like to shut up compiler warnings with casts and that's >>> why C's approach is better. >> >> The code in question was: >> >> double d; >> int *pi = &d; >> >> and the question is whether a compiler should reject it with an error >> message or accept it with a mere warning. >> >> Personally, I would never deliberately write that. As far as the C >> standard is concerned, it's a constraint violation, requiring a >> diagnostic from any conforming compiler. If a compiler issues a >> warning and continues to compile the code, it might seem reasonable >> to assume that it will generate a conversion from `double*` to `int*` >> -- but in fact the language standard does not say or imply that >> that's what will happen. The behavior is undefined; a compiler could >> legally generate code that sets `pi` to a null pointer, for example. > > I didn't knew that this is undefined behavior. > >> >> Whereas if I *want* to do that conversion, there's a perfectly valid >> way to do it that likely won't result in a warning: >> >> double d; >> int *pi = (int*)&d; >> > > Problem is that we wont get warning in this case IMO. You shouldn't. A cast is a way of saying "I'm doing something dangerous. Trust me to know that I'm doing it right". If you don't know that the conversion is safe, you shouldn't write the cast in the first place. If you do know it's correct, a warning message would be inappropriate and distracting. If you have a problem that you've traced into piece of code containing a cast, you should always consider the possibility that the cast is the problem. >> A problem I have with gcc is that it doesn't clearly distinguish >> between warnings about constraint violations, like the above, and >> warnings about legal but questionable code, such as writing `if (x = >> 42)` rather than `if (x == 42)`. Again, this doesn't violate the C >> standard; I just personally find it annoying. >> >> That's just my opinion (that all syntax errors and constraint >> violations should be treated as fatal errors, not as warnings -- or >> at least that there should be an option that works that way). What's >> yours? Which errors do you think should be fatal, and which should >> merely trigger a warning? > > I don't think that converting one pointer type to other is error. The C standard says otherwise. More precisely, depending upon the context, it is sometimes a constraint violation, and sometimes has undefined behavior for other reasons. > It seems to me that in spirit of C it should be allowed as it is. > It should issue error if types are incompatible, say pointer to int > has less bytes than pointer to double. The types are incompatible in this case, which is precisely what's wrong with this code. I think you're using a different meaning for "compatible types" than the C standard uses. Pointers to incompatible types are themselves incompatible pointer types. Types of different sizes are never compatible, regardless of which one is bigger, but types of the same size can be incompatible too. > Problem is that if code that uses such conversions will work at all. > Say copying data as int / reading as double. > It might work in one case and might not in other. > It would be very difficult to find out bug (wrong result etc) without > compiler warning IMO. Keep in mind that, because the code contains a constraint violation, the compiler is free to refuse to compile it, even if you're absolutely certain that the code is safe. You'll have your compiler warning, but you won't have a bug, because you won't have an executable. If you're not certain, don't write such code. If you are certain, you may need the cast to force your compiler to accept it.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-21 20:36 +0000
Re: Function factories Nobody <nobody@nowhere.com> - 2012-08-21 21:55 +0100
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 16:36 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 13:01 -0400
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 18:33 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 22:26 -0400
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 08:34 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-22 01:52 -0700
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 11:34 +0200
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:30 +0100
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:29 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-22 12:32 +0200
Re: Function factories tom st denis <tom@iahu.ca> - 2012-08-22 07:40 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 11:05 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 11:08 +0200
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 13:27 +0100
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 11:13 -0400
Re: Function factories "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-23 18:18 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:40 -0400
Re: Function factories Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-08-23 09:29 -0700
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 18:44 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:46 -0400
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 18:02 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 19:24 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 18:07 -0700
Re: Function factories Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-23 21:30 -0400
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 21:46 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 13:35 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 20:13 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 17:57 -0700
Re: Function factories Andrew Cooper <amc96@cam.ac.uk> - 2012-08-25 02:35 +0100
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 21:21 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 15:36 -0400
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 22:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 16:20 -0400
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-23 22:33 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 17:07 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 14:51 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 00:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 19:01 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 16:31 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 18:31 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-24 02:37 -0700
csiph-web