Common malloc/free practice violates ANSI standard ?

T. William Wells bill at twwells.com
Wed Oct 25 05:49:20 AEST 1989


In article <423 at cpsolv.UUCP> rhg at cpsolv.uucp (Richard H. Gumpertz) writes:
: In article <1989Oct19.101306.16791 at twwells.com> bill at twwells.com (T. William Wells) writes:
: >The standard requires that casting from (void*) to (OBJ*) and back
: >give pointers that compare equal and point to the same object.
: >Free will have no problems with it.
:
: WRONG!

Bull. Maybe a demonstration is in order? And then maybe you'll
stop nitpicking out of context?

4.10.3

"The pointer returned if the allocation succeeds is suitably
aligned so that it may be assigned to a pointer of any type of
object and then used to access such an object in the space
allocated (until the space is explicitly freed or reallocated).
Each such allocation shall yield a pointer to an object disjoint
from any other object. The pointer returned points to the start
(lowest byte address) of the allocated space."

Thus the malloc yields "a pointer to an object", presented as a
void*, which may then be converted to a pointer to said object.

3.3.4:

"A pointer to an object or incomplete type may be converted to a
pointer to a different object type or a different incomplete type.
The resulting pointer might not be valid if it is improperly
aligned for the type pointed to. It is guaranteed, however, that
a pointer to an object of a given alignment may be converted to a
pointer to an object of the same alignment or a less restrictive
alignment and back again; the result shall compare equal to the
original pointer. (An object that has character type has the
least strict alignment.)"

Thus the object pointer may be converted back to a void*, since
the void* is guaranteed to not have a more restrictive alignment
(by 3.1.2.5 and 3.3.4).

3.3.9:

"If two pointers to object or incomplete types compare equal,
they point to the same object. If two pointers to functions
compare equal, they point to the same function. If two pointers
point to the same object or function, they compare equal."

Since casting does not change the object pointed to, the new
void* must compare equal to the original void*.

QED.

: This is getting tiring.  The standard requires that casting OBJ * to void *
: and then back to OBJ * preserve the value.  It does NOT (and intentionally
: so) require the reverse.

You have been tiring. You keep making your assertion, without
evidence. Now you have the evidence that you are full of shit.

Go away, so that the bandwidth may be used by something more
interesing. Goto, anyone? :-(

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com



More information about the Comp.lang.c mailing list