Richard Heathfield said:
CBFalconer said:
What was unclear about my previous contributions to this thread?
Here's the article you wrote, to which I posted my (attempted)
clarification:
| Random832 said:
|
| > 2006-12-12 <
[email protected]>,
| > Richard Heathfield wrote:
| >> Under that circumstance, the compiler is obliged to assume that malloc
| >> returns int,
| >
| > Under which circumstance, in turn, the compiler is not "obliged" to
| > anything in particular at all.
|
| "If the expression that precedes the parenthesized argument list in a
| function call consists solely of an identifier, and if no declaration is
| visible for this identifier, the identifier is implicitly declared exactly
| as if, in the innermost block containing the function call, the declaration
|
| extern int identifier();
|
| appeared." (C89: 3.3.2.2)
Your citation of the standard appeared to me to be a refutation of
Random832's statement that
the compiler is not "obliged" to anything in particular at all.
In other words, you seemed (based only on that one followup, without
reference to previous articles in the thread) to be asserting that,
given:
/* Note: no "#include <stdlib.h>" */
int main(void)
{
int *p = (int*)malloc(sizeof(int));
}
the compiler is obliged to treat malloc() as a function returning int
(and the int result will be converted to int*). In fact, as we both
know but others may not, the call invokes undefined behavior, and the
implementation is therefore free to do anything it likes -- including
treating malloc() as a function returning void* if the implementer is
more interested in generating correct results for bad code than in
emitting good diatnostics.
The fact that the call invokes undefined behavior is obvious to both
of us, but possibly not to others. The fact that the undefined
behavior implies that the compiler is allowed to treat malloc() as a
function returning void* is sufficiently subtle that it seemed
plausible you might have missed it -- thus my clarification.
(In fact, if a compiler is clever enough to recognize the undefined
behavior because it know about "malloc" even without the header, it
had *better* issue a decent diagnostic rather than just quietly
generating code consistent with the user's presumed intent, but that's
a quality of implementation issue.)