Re: Program crashes when running it outside dev environment
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 08 Jun 2007 23:33:17 +0000
David Tiktin said:
On 08 Jun 2007, Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:
David Tiktin said:
On 08 Jun 2007, Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:
So my suggestions would be:
1) crank up your warning level to the max
2) turn off any extensions you can live without
3) fix every single diagnostic message, and *never* with a cast
I'm with you right up to the last clause. Are you saying you
never actually *need* a cast?
No, there are (rare) occasions when you do need a cast. But I
don't know of any occasion where you *need* a cast AND omitting it
violates a constraint or constitutes a syntax error. So adding a
cast is not the right way to fix a diagnostic message. If the
choice is between adding bad code and putting up with a bad
warning, I'll live with the bad warning.
But now you're changing your advice.
Yes, I am, aren't I? I don't recall signing a contract... :-)
I think my best defence (which is intended sincerely but might sound a
bit self-serving and pompous, for which I apologise in advance) can be
found in "The Tao of Programming", by Geoffrey James:
"There once was a master programmer who wrote unstructured programs. A
novice programmer, seeking to imitate him, also began to write
unstructured programs. When the novice asked the master to evaluate his
progress, the master criticized him for writing unstructured programs,
saying, ``What is appropriate for the master is not appropriate for the
novice. You must understand the Tao before transcending structure.''"
Actually, I try not to write unstructured programs! But my point is
this: a relative newcomer to C is generally less skilled at
interpreting diagnostic messages than someone who has been in the game
for a few years. The temptation newcomers often have is to keep fixing
stuff *until it compiles*, at which point they tend to dismiss any
remaining warnings as "only warnings", whereas they (and their
programs) would benefit greatly from treating *every* diagnostic
message as being important. Over time, they will learn which messages
truly can be overlooked and which cannot. In the meantime, adopting a
policy of "cast away the diagnostic" is likely to lead to bad code and
sloppy habits.
You get diagnostics for things
other than constraint violations and syntax errors. On the highest
warning levels, you may well get a diagnostic on code like this:
char * ptr = buffer;
int c;
while ((c = getchar()) != EOF)
{
*ptr++ = c;
}
diag.c(73) : warning C4244: '=' : conversion from 'int ' to 'char ',
possible loss of data
That's with MSVC 6.0 and -W4.
Sure. Would you add a cast there? I wouldn't.
Do you think loss of precision diagnostics are "bad warnings"? (I
don't.)
No, but they can be a nuisance at times! (And this, indeed, is one such
time.)
Shouldn't we fix this with a cast?
No. How does it fix the code? Does it remove the loss of precision? Of
course not!
*ptr++ = (char) c;
If not, what do you suggest? If we do nothing, we'll have to think
about that warning every time we build,
Add a comment to the code, explaining why no loss of precision is
involved.
because an *actual* loss of precision is often a serious bug.
So why hide it with a cast?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: Program crashes when running it outside dev environment
- From: David Tiktin
- Re: Program crashes when running it outside dev environment
- References:
- Program crashes when running it outside dev environment
- From: z
- Re: Program crashes when running it outside dev environment
- From: Richard Heathfield
- Re: Program crashes when running it outside dev environment
- From: David Tiktin
- Re: Program crashes when running it outside dev environment
- From: Richard Heathfield
- Re: Program crashes when running it outside dev environment
- From: David Tiktin
- Program crashes when running it outside dev environment
- Prev by Date: Re: Program crashes when running it outside dev environment
- Next by Date: Re: Program crashes when running it outside dev environment
- Previous by thread: Re: Program crashes when running it outside dev environment
- Next by thread: Re: Program crashes when running it outside dev environment
- Index(es):
Relevant Pages
|