RFH: aliasing problems

Sergei Organov osv at javad.com
Tue Dec 12 08:58:06 UTC 2006


Steven Johnson
<sjohnson at sakuraindustries.com> writes:

> Sergei Organov wrote:
>> Steven Johnson
>> <sjohnson at sakuraindustries.com> writes:
>>
>>> Hi,
>>>
>>> Can anyone explain how -fstrict-aliasing is compatible with C99 Section
>>> 6.3.2.3(7).
>>>
>>> because it says:
>>>
>>> "A pointer to an object ... may be converted to a pointer to a different
>>> object ... when converted back again the results shall compare equal to
>>> the original pointer."
>>>
>>> If this is not the definition of type punning with pointers, I do not
>>> know what is.
>>>
>>
>> This tells nothing about semantics of accessing corresponding object
>> by dereferencing the pointer, while aliasing is all about accessing actual
>> objects using pointers of different types.
>>
> The full quotation is:

Steven, I'm not sure what do you try to achieve here. The strict
aliasing rules are there in the C99 standard. We may like or dislike this
fact, but it is the fact, so a program violating the strict
aliasing rules is not C99-compliant. Full stop.

Here are the aliasing rules:

  An object shall have its stored value accessed only by an lvalue
  expression that has one of the following types:

  - a type compatible with the effective type of the object,

  - a qualified version of a type compatible with the effective type of
    the object,

  - a type that is the signed or unsigned type corresponding to the
    effective type of the object,

  - a type that is the signed or unsigned type corresponding to a
    qualified version of the effective type of the object,

  - an aggregate or union type that includes one of the aforementioned
    types among its members (including, recursively, a member of a
    subaggregate or contained union), or

  - a character type.

You argue that if they put aliasing rules, they could as well disallow
pointer types conversions altogether, but that's incorrect.
Aliasing rules don't make all type punning with pointers
invalid, so disallowing pointer types conversion is not the same as
enforcing aliasing rules. For example, aliasing rules allow to access
an object of any type with a char* pointer, and that wouldn't be
possible without allowed conversions of pointer types.

-- Sergei.




More information about the users mailing list