Common malloc/free practice violates ANSI standard ?

Lloyd Kremer kremer at cs.odu.edu
Fri Oct 27 05:50:56 AEST 1989



I will try to shed some additional light on this, although I may be a fool
to attempt it.  :-)

There is a fundamental difference between
	void *
and
	void *  returned by malloc (or any allocation routine).

In one sense they are opposites.  A void* in general has the most lenient
alignment requirements of any data pointer.  A void* returned by malloc has
the most stringent.

	OBJ* -> void* -> OBJ*
is true for any void*.

	void* -> OBJ* -> void*
is not true for general void*, but is guaranteed true for void* returned
by malloc.

void* is required to be physically identical to char*.  In some
implementations (word oriented architectures), a char* is physically bigger
(has more bits) than pointers to some other objects.  A char* contains a
word address and then a byte offset within the word.  Hence a cast from
a general void* to a smaller pointer type can irreversibly lose data.
A void* returned from malloc is guaranteed to have these additional bits
zero, so the truncation is harmless.

					Lloyd Kremer
					kremer at cs.odu.edu
					Have terminal...will hack!



More information about the Comp.std.c mailing list