E. Robert Tisdale said:
Eric said:
A cryptic and possibly misleading diagnostic
is more helpful than no diagnostic at all.
In fact, this entire thread stems from an incident
in which the unnecessary cast hid the diagnostic
that the compiler would otherwise have been required to produce!
The O.P.'s mistake in omitting [the declaration of malloc]
went unnoticed exactly as we've all said would happen.
You are confused. No diagnostic was hidden.
You need to reread Lionel Valéro's original post.
The diagnostic message was,
"warning: cast to pointer from integer of different size".
No, the confusion is elsewhere. The O.P. asked why his
program compiled without complaint in one environment and
elicited a diagnostic on another. Without the cast, the
program would have produced diagnostics on *all* C compilers.
Thus, the cast hid the required diagnostic, and it was pure
luck that an "optional" or "extraneous" diagnostic from another
compiler drew his attention to the error.
It says nothing about the fact that malloc was not declared.
It certainly doesn't tell you that stdlib.h was not included.
Nor does it tell you how to feed the hungry, heal the sick,
and end war. And when the diagnostic is suppressed altogether,
as in the O.P.'s original environment, it doesn't even tell
you to come in out of the rain.
But if the diagnostic had been permitted to appear, it
would at least have alerted the O.P. to the existence of a
problem. The description of the problem might not have been
all that could be desired, but knowing that a problem exists
is at least half the battle in finding the cure.
My GNU C compiler
gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
issues a more informative diagnostic message:
malloc.c:8: warning: implicit declaration of function `malloc'
That's nice. Seriously, it *is* nice. So what?
The solution to this problem is to get a better C compiler
and *not* to cobble your code to accommodate inferior C compilers.
The "inferior compilers" in the O.P.'s case were (ahem)
gcc and gcc. And I fail to see how leaving out an unnecessary
and potentially harmful cast constitutes "cobbling" the code.
It allows you to compile your code with a C++ compiler.
This is important because it allows your C code to survive
even if the C language itself doesn't survive.
I once saw a cute little "Hello, world!" program that
ran correctly in three different languages: C, Fortran, and sh,
if I recall aright. It was an amusing stunt, but hardly a useful
way to write programs ...
I put it to you that writing programs that are somehow
simultaneously both C and C++ is an exercise of similar
[f]utility. Yes, it's possible to write such code, if one
is willing to wear a tight enough strait jacket. But the
result will be derided by both camps: Neither the C people
nor the C++ people will deem it good code. In the particular
case of malloc(), the C++ folks will say it's poor practice
to use it in the first place; the `new' operator needs no
cast. If you want to write C++ go right ahead and do so --
but don't pretend that writing bad C code so it can masquerade
as bad C++ code is a good idea.
Personally, I use `new' as an identifier whenever I can
find a reasonable excuse to do so.