_Addresses_Add_offset and friends and strict aliasing

Sergei Organov osv at javad.com
Fri Dec 15 10:11:55 UTC 2006


Sergei Organov <osv at javad.com> writes:
[...]
> Not changing pointer type definitely doesn't violate aliasing rules, so
> if you convert result of the function back to the same pointer type
> you've passed, you even have no chances to violate aliasing rules
> because of subtracting an offset, be it 0 or any other value.

I think I need to correct myself here. If offset is non-0 it might
happen that the resulting pointer now points to something entirely
unrelated to the type of the pointer, so aliasing violation may in fact
happen even when pointer type is not changed. In a way similar to this:

struct S {
  int a[2];
  long v;
} s;

int foo()
{
  s.v = -1; 
  return s.a[2];
}

I think s.a[2] violates aliasing rules as it accesses s.v of type long
using lvalue of type int.

-- Sergei.




More information about the users mailing list