[comp.lang.c] pointers, alignment, etc

chris@mimsy.UUCP (Chris Torek) (08/18/89)

>In article <18996@mimsy.UUCP> I noted that, given
>>	char *cp;
>>	int *ip;
>>	ip = cp;
an `ANSI C compiler' (not that any exist yet; the standard is still in
the `proposed' state)
>>must issue some kind of diagnostic ....

In article <597@targon.UUCP> ruud@targon.UUCP (Ruud Harmsen) writes:
>I suppose this is machine-dependent because of alignment: char-pointers can
>point to just about anywhere, but int-pointers on many machines have to be
>aligned properly.

This is at least one reason, if not the only reason.  I know of no other
reason, and the (proposed) standard makes various constraints that may
eventually prove to eliminate other objections to such pointer conversions.

>My question is: can I make sure in my program, that though generally
>non-portable this IS portable?  I tried this once in the following way:
>The char-pointer gets its value from malloc, which the manual says gives
>pointers properly aligned for any type.  I never change that char-pointer
>other than by adding multiples of sizeof(int) to it.
>Is a "ip = cp" guaranteed safe under these conditions, so can I ignore
>the compiler-warning?

I believe it is safe, which is not the same as its being safe.  (It is
much harder, given some property P, to prove based on the proposed
standard that, for all x, P(x) holds.  In general, it is easier to come
up with some y for which P(y) does not hold.  Here I cannot think of
any such y.  Coming up with y's [or maybe they are y-nought's :-) ]
is good exercise for those who propose adding features to the standard.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris