Re: Unknown function



Christian Christmann wrote:
It says: "... the compiler actually does not "know" the signature of
malloc(), hence it assumes by default, that it returns int..."

How can the function call of 'malloc' work at all if it is unknown?
I thought that each function that is unknown to the compiler at a specific
point of the program leads to an 'undefined reference' when tried
to get linked.

No (at least not in C89)

If there is no definition or prototype the compiler assumes (because
the standard tells it to) that malloc() takes int arguments and
returns an int. As you clearly understand, this is going to be a
problem since malloc actually takes size_t and returns void *. Since
20 is a valid value to pass to a function that takes either int or
size_t, the compiler doesn't know that the argument is wrong, but it
does know that the return value is wrong, hence the warning.

It is not unusual for the code to work on a particular implementation,
since size_t is often a typedef for int, and pointers may be
implemented as integers of the same size as int. When it doesn't work,
it can just as easily be because the argument type is wrong as because
the return type is wrong. For example, on 64-bit hardware, void * and
size_t may both be 64-bit types and int a 32-bit type. The reason the
compiler complains [and is required to complain] about the return value
but not the argument is that the return value problem is easily
detectable.

The warning is misleading, since it suggests that adding a cast would
solve the problem. This is not true in general and is very unlikely to
be true in practice. Writing
p = (char *) malloc(20);
is likely to make the warning go away, but if void * return values are
passed differently than ints
or are a different size then the cast is not going to magically
retrieve the lost information. This code doesn't say "malloc returns a
char *", it says "although malloc returns an int, I want the compiler
to pretend that it is a char *". For this to be helpful you need a
strange implementation where void * and int are stored and returned
from functions in the same way but char * is stored differently.

A more helpful compiler message might mention that the return value of
int came from the "implicit int" rule rather than an actual
declaration. Something like: "use of undeclared function malloc() does
not match implicit declaration". However, it could be worse. The
Standard permits the compiler to give even less helpful messages such
as "There might be a problem with line 42".


-thomas

.



Relevant Pages

  • Re: about the array
    ... 3- If you cast to the wrong type by accident, ... are malloc and free. ... the compiler should issue a diagnostic - gcc ... unknown set of arguments, and return an int. ...
    (comp.lang.c)
  • Re: malloc
    ... the compiler is obliged to assume that malloc ... int main ... the compiler is obliged to treat malloc() as a function returning int ... The fact that the call invokes undefined behavior is obvious to both ...
    (comp.lang.c)
  • Re: Segmentation fault on 64 bit
    ... using a HP-UX C compiler on PA-RISC system. ... malloc() and friends are defined. ... int main(int argc, char **argv) ... Obviously a pointer can't be stored in an int on the system ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... Why do you lie to the compiler? ... you define later takes an argument, a pointer to pointer to ... Under both Windows and Linux mainalways returns an int (and ... not know about the return type of malloc(). ...
    (comp.lang.c)
  • Re: Confused with malloc
    ... malloc without prototyppe gets ... interpreted as int malland this gets you code like: ... Not every compiler uses one and the same location with exactly the ... same number of bits for int and pointer to something. ...
    (comp.lang.c)