Re: Unknown function



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.
.



Relevant Pages

  • Re: pointers?
    ... int foo ... If the compiler takes the effort to actually recognize that this is ... addresses do not work like in a flat address space. ... "contiguous region" in the standard document. ...
    (comp.lang.forth)
  • Re: Arithmetic on function address
    ... > need not be a valid value for an `int', ... generates does the conversion and actually returns the address of the ... compilers needing to give more than just what the standard says) either ... compiler are the same size as an int, ...
    (comp.lang.c)
  • Re: Unknown function
    ... int main ... message) "the compiler actually dos not "know" the signature of malloc()", I understand it in the way that malloc is not known to the compiler at that specific point of program. ... What the standard mandates that the compiler must do and what the compiler can do in addition to what the standard mandates. ...
    (comp.lang.c)
  • Using restricted pointers with newly allocated arrays/structures
    ... return (int) x; ... The use of "restrict" here is intended to inform the compiler that x ... However, according to 6.7.3.1p4 of the standard, the code seems to ... based on restricted pointer x, is used to access the object x(and ...
    (comp.std.c)
  • Re: byte, int....professor responds, confusion increases.
    ... > I'm am taking an introduction to Java class at my university and I'm ... > We are currently going over primitive data types and data conversion. ... Could you help me understand why the compiler is ... > The constant, 10, is considered to be of type int. ...
    (comp.lang.java.help)