Ad
  • Custom User Avatar

    cppreference on strict aliasing

    Given an object with effective type T1, using an lvalue expression (typically, dereferencing a pointer) of a different type T2 is undefined behavior, unless:

    • T2 and T1 are compatible types.
    • T2 is cvr-qualified version of a type that is compatible with T1.
    • T2 is a signed or unsigned version of a type that is compatible with T1.
    • T2 is an aggregate type or union type type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union).
      cppreference's definition of an aggregate type
      aggregate types: array types and structure types

    Since the type of shape is a struct that contains (directly or recursively) a member of type shape_t, the cast does not violate strict aliasing rules, as struct types are aggregate types.
    (At least according to the definitions on cppreference)

    Furthermore, since the type of shape is a struct that contains (directly or recursively) a member of type shape_t, the alignment of shape is guaranteed to meet the alignment requirements of shape_t.

    Lastly, since the very first member of the type of shape (directly or recursively) is of type shape_t, the offset to that member is guaranteed to be 0.

    Thus, that pointer cast is valid.

  • Default User Avatar

    closing to keep a single issue

  • Custom User Avatar

    Reraised as issue

  • Default User Avatar

    You can do a hack fix and just do a guard that returns Just 0 if the item is negative

  • Default User Avatar

    Disclaimer: one cannot simply understand aliasing, so I'm not entirely sure.
    Given that (const shape_t *)shape is &((const right_triangle_t *)shape)->shape, the underlying type shape_t is the same, so there's no aliasing. The only question is whether the type cast is valid. So there's C11 ("n1570.pdf") 6.7.2.1.15:

    Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

  • Custom User Avatar

    Fixed. Thanks ;-)
    And sorry for late(I haven't seen this issue report before today)

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Good point, I guess for no reason.
    I was probably using it for something else in which I needed to specify that a is Fractional, and then I just left it as it was :P

  • Custom User Avatar

    Fixed

  • Default User Avatar

    No issue. This is part of the task.
    (Generally you are right, of course. ;-))

  • Default User Avatar

    Random tests added.

  • Default User Avatar

    Thanks,
    good advice,
    I'll add random tests

  • Default User Avatar

    Thanks for your post!

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar