void *

KW Heuer kwh at bentley.UUCP
Fri Apr 25 04:59:13 AEST 1986


In article <200 at brl-smoke.ARPA> gwyn at BRL.ARPA (VLD/VMB) writes:
>You're mixing two features of malloc().  Malloc() should return (void *),
>which is a generic pointer type to/from which all other pointer types
>may be cast without loss of information.  (This is the only use of
>(void *), and it is the only universal pointer type; other pointer
>casts can be non-portable.)  Malloc() ALSO guarantees that the pointer
>it returns is suitably aligned for safe casting to ANY pointer type.
>This is a property of malloc(), not of (void *).

I realize that; it's just that I was expecting the new datatype to
correspond to this property of malloc(), so malloc() wouldn't have to
be a special case.  I probably shouldn't have brought up this (ALIGN *)
issue in the same posting.

It's *not* true that (void *) can always be cast into another pointer
type without loss of information.  For example, "intp = voidp;" may
drop bits if "voidp" was initialized to an improperly aligned object.
What's true is that any pointer may be assigned *to* a void pointer,
and that *same* value may be safely copied back.  (Currently this is a
guaranteed feature of (char *).)

To rephrase my original question, what does this have to do with the
explicit use of the cast operator?  Is "intp = voidp;" going to be as
acceptable as the (more correct, to me) syntax "intp = (int *)voidp;"?

Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint



More information about the Comp.lang.c mailing list