commom malloc/free practice breaks standard - author strikes back

Scott A. Rotondo scott at altos86.Altos.COM
Tue Oct 24 07:50:01 AEST 1989


In article <420 at cpsolv.UUCP> rhg at cpsolv.uucp (Richard H. Gumpertz) writes:
> Arguments that just make assertions without citation don't hold much water.
> I cannot find anything that says conversion from "void *" to "obj *" cannot
> round up.  In fact,I cannot find anything in the standard that says
>       free((void *) (object *) malloc(sizeof(object)))
> will work: the standard only says that
>       free(malloc(sizeof(object)))
> will work.  Please tell me what section I have overlooked or stop saying
> the standard already has things that I cannot find.

1. No one (to my knowledge) has claimed that conversion from "void *"
to "obj *" is allowed to change the pointer's address if the "void *"
is already aligned according to the requirements for "obj *".

2. Section 4.10.3 guarantees that the "void *" returned by malloc() is
already aligned according to the requirements for "obj *", no matter
what type "obj" represents.  Therefore, no conversion from "void *" to
"obj *" and back to "void *" can possibly change the pointer's
address.

Please note that the phrase "change the pointer's address" includes
"rounding up" the address, but it does not include changing the format
of the pointer without changing the address to which it points.  The
format of the pointer is irrelevant because it is visible only to the
implementation.

Anyone who wishes to continue this discussion will have to make the
claim mentioned in paragraph 1 above, that a cast can modify a pointer
address even if it is already aligned.  I caution anyone who considers
making this claim to read the Standard first.
-- 
===============================================================================
Scott A. Rotondo, Altos Computer Systems			 (408) 946-6700

{sun|pyramid|uunet}!altos86!scott				scott at Altos.COM	



More information about the Comp.std.c mailing list