void * [also malloc]
Sam Kendall
sam at delftcc.UUCP
Sat Apr 26 02:21:38 AEST 1986
In article <200 at brl-smoke.ARPA>, gwyn at BRL.ARPA writes:
> 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.)
Can anyone tell me when a `void *' could have a different representation
than a `char *'? Since `sizeof (char)' is always 1 according to both
practical considerations and the draft standard, a char must be the
smallest addressable object. Also according to practical considerations
(and the draft standard, I think) all objects consist of an integral
number of chars. So there is nothing a `void *' can point at that a
`char *' cannot.
A `void *' and a `char *' could have different representations, although
I cannot think of a good reason. But I think casts to `char *', and
from `char *' with proper alignment, are portable. `void *', then,
is just a convenience for the human mind and for typecheckers. That's
okay with me.
> 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've always wondered about something here: why is the return value of
malloc guaranteed to be aligned for any object, even one bigger than
the allocated area? Currently, `malloc(1)' must return a maximally
aligned pointer! This prevents an implementation which does something
space-efficient with small allocations.
----
Sam Kendall { ihnp4 | seismo!cmcl2 }!delftcc!sam
Delft Consulting Corp. ARPA: delftcc!sam at NYU.ARPA
More information about the Comp.lang.c
mailing list