Linux and aliasing?

Martin v. Loewis
Wed Jun 30 15:43:00 GMT 1999

> For all I know, this problem is the result of C, or gcc, being
> too permissive about allowing casts across pointers to different
> types...

The problem is that ISO C explicitly allows you to cast pointers
forwards and backwards to completely unrelated types. Only when
you *dereference* the pointer, you must be consistent in the type,
or derefence through char*.

If you have a sequence

long foo()
   long a = 15;
   long *b = &a;
   void *c = b;
   float *d = c;
   *d = 3.14;
   return a;

then the standard says that this code has undefined behaviour, yet
every single statement is ok. The compiler could take the position
that d cannot possibly alias with b, and return 15. (In this example,
analysis detects that they are aliased, anyway)

Now, there are proposals to relax the rules. There is
-fno-strict-aliasing; I can't really see how there is an in-between.


More information about the Gcc mailing list