Re: C/C++ guidelines
- From: "Peter J. Holzer" <hjp-usenet2@xxxxxx>
- Date: Mon, 17 Sep 2007 00:45:06 +0200
On 2007-09-16 21:46, Mark McIntyre <markmcintyre@xxxxxxxxxxx> wrote:
On Sun, 16 Sep 2007 12:00:21 +0200, in comp.lang.c , Karl Heinze
<nomail@invalid> wrote:
On Sat, 15 Sep 2007 22:45:00 -0400, CBFalconer <cbfalconer@xxxxxxxxx>
wrote:
Huh?Failure to #include <stdlib.h> going undetected.[...] in C you should not cast the result of malloc, becauseFor example?
doing that might introduce subtle errors [...]
So it's _not_ the usage of the cast (as such) which "might introduce
subtle errors" but forgetting to include <stdlib.h>.
Splitting hairs. If the cast were not there, you'd be warned about the
missing prototype.
No, you'd be warned about a conversion from integer to pointer.
By inserting the cast, you mask the other error.
Maybe, maybe not. If the compiler warned about a missing prototype, it
would do so with or without the cast.
On the other hand most modern C compilers (as it seems) would catch
that lapse (and produce a warning).
Its a warning and (in C89 at least) no diagnosis is actually required.
So you're relying on Quality of Implementation.
If you rely on getting a useful diagnostic for an implicit conversion
from int to a pointer, you are also relying on Quality of
Implementation. The standard only says that "a diagnostic" is required,
but doesn't require any useful information to be included in the
diagnostic. "Warning: This program may contain errors" is perfectly
acceptable as far as the standard is concerned.
You may have a point if you claim that almost all compilers do warn
about the implicit conversion while only a few warn about the implicit
declaration, but you would have to back up this claim with facts.
As a tiny counter example, I submit that gcc 4.1.2 without any -W*
options warns:
foo.c:3: warning: incompatible implicit declaration of built-in function
‘malloc’
both with and without the cast. It doesn't warn about the implicit
conversion in this case (but it does if I replace "malloc" with "foo").
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@xxxxxx |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
.
- Follow-Ups:
- Re: C/C++ guidelines
- From: Mark McIntyre
- Re: C/C++ guidelines
- References:
- Re: C/C++ guidelines
- From: Karl Heinze
- Re: C/C++ guidelines
- From: CBFalconer
- Re: C/C++ guidelines
- From: Karl Heinze
- Re: C/C++ guidelines
- From: Mark McIntyre
- Re: C/C++ guidelines
- Prev by Date: Re: Question on device driver ?
- Next by Date: Re: Eventual undefined behaviour
- Previous by thread: Re: C/C++ guidelines
- Next by thread: Re: C/C++ guidelines
- Index(es):
Relevant Pages
|