Re: gcc knows about malloc()
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Wed, 14 Jun 2006 07:55:39 -0400
Old Wolf wrote:
Keith Thompson wrote:
Consider the expression
malloc
If a correct prototype is visible, this expression (after the implicit
conversion that's done to function names in most contexts) is
void *(*)(size_t)
Casting this expression to the same type, of course, performs no
actual conversion and simply yields the same pointer-to-function
value.
But in the absence of a prototype, the compiler doesn't know the type
of the expression
malloc
so it cannot, in the general case, generate correct code to convert
this value to type void *(*)(size_t). It's likely to generate code
that would convert a value of type int (*)() to type
void *(*)(size_t); applying this code to a value that's *actually* of
type void *(*)(size_t) unleashes the nasal demons.
Seems to make sense. Is there any documentation in the
standard to back this up?
Personally I'm not convinced that the code should even
compile. In C99 it certainly doesn't: C99 does not have
implicit function declaration.
Keith's argument can also be illustrated with
#include <stddef.h>
long malloc(const char*, double, struct foo*);
void * (*pseudo_malloc)(size_t) = malloc;
.... which does not rely on implicit int.
After some pondering, I think Keith is right. His
argument is really the same as that for other kinds of
type mismatches, like
/* file1.c */
double trouble;
/* file2.c */
extern int trouble;
...
double toil = (double)trouble;
Even though the compiler of file2.c is told to convert the
value of `trouble' to the type of `trouble's definition, it's
been misinformed about the "starting point" for the conversion
and (probably) generates incorrect code.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: gcc knows about malloc()
- From: Harald van Dijk
- Re: gcc knows about malloc()
- References:
- gcc knows about malloc()
- From: James Dow Allen
- Re: gcc knows about malloc()
- From: Mark McIntyre
- Re: gcc knows about malloc()
- From: Jordan Abel
- Re: gcc knows about malloc()
- From: Mark McIntyre
- Re: gcc knows about malloc()
- From: Eric Sosman
- Re: gcc knows about malloc()
- From: Mark McIntyre
- Re: gcc knows about malloc()
- From: Old Wolf
- Re: gcc knows about malloc()
- From: Keith Thompson
- Re: gcc knows about malloc()
- From: Old Wolf
- gcc knows about malloc()
- Prev by Date: Re: Shift operators.
- Next by Date: C call tree
- Previous by thread: Re: gcc knows about malloc()
- Next by thread: Re: gcc knows about malloc()
- Index(es):
Relevant Pages
|
Loading