This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: type based aliasing again
- To: craig at jcb-sc dot com
- Subject: Re: type based aliasing again
- From: "Ross Morgan-Linial" <rmorgan at jetcity dot com>
- Date: Sat, 18 Sep 1999 02:49:47 -0700
- CC: gcc at gcc dot gnu dot org
> Not in this respect, apparently. Nobody has, to my knowledge,
> proposed that -traditional imply -fno-alias-analysis, which suggests
> K&R I indeed, as has been reported, left this behavior undefined.
Well, a quick flip through my copy reveals what look to me like
contradictory statements. In chapter 5 (pointers and arrays) it says
You should also note the implication of the declaration that a
pointer is constrained to point to a particular type of object.
Now, that appears to say that playing games with pointers is right
out. However, in appendix A (C reference manual), section 14.4, it
says
A pointer to one type may be converted to a pointer to another
type. The resulting pointer may cause addressing exceptions upon
use if the subject pointer does not refer to an object suitably
aligned in storage. It is guaranteed that a pointer to an object
of a given size may be converted to a pointer to an object of a
smaller size and back again without change.
For example, a storage-allocation routine might accept a size (in
bytes) of an object to allocate, and return a char pointer; it
might by used in this way.
extern char *alloc();
double *dp;
dp = (double *) alloc(sizeof(double));
*dp = 22.0 / 7.0;
alloc must ensure (in a machine-dependent way) that its return
value is suitable for conversion to a pointer to double; then the
_use_ of the function is portable.
That seems to imply that it's okay to use a pointer pointing to an
object of different type as long as it's suitably aligned. Of course,
I'm hardly an expert on K&R C.