Re: Unknown function
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sun, 12 Mar 2006 11:40:30 GMT
Barry Schwarz <schwarzb@xxxxxxxxx> writes:
On Sun, 05 Mar 2006 22:58:33 +0100, Michael Mair[...]
<Michael.Mair@xxxxxxxxxxxxxxx> wrote:
Without prototype in scope, malloc() indeed is assumed to return int.
This is only true in the old standard. The current standard no longer
supports this assumption.
But even a C99 compiler is free to do so, as long as it issues a
diagnostic. It's likely to allow it for backwards compatibility.
The conversion from int to void * or backwards is allowed;
however, it is not guaranteed to be without loss. As char * and void *
While this is reasonably true, it is irrelevant. Allowing the
compiler to believe the function returns a different type than it
actually does leads to undefined behavior. Think about systems where
a pointer is returned in one type of register and an int in another or
where pointers and ints have different endian-ness.
Right. A conversion can occur only when the compiler knows the type
of the expression and the type to which it's converted. If you call
malloc() with no visible prototype, the compiler doesn't have enough
information to perform a conversion. At best, it treats the bits of
the void* result *as if* they were an int representation (whereas a
conversion might involve a change of representation); at worst,
arbitrarily bad things can happen.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Unknown function
- From: Christian Christmann
- Re: Unknown function
- From: Michael Mair
- Re: Unknown function
- From: Barry Schwarz
- Unknown function
- Prev by Date: Re: give me some tips
- Next by Date: Re: unexpected result
- Previous by thread: Re: Unknown function
- Next by thread: Re: Unknown function
- Index(es):
Relevant Pages
|