1

Something weird I thought of when reading up on strict aliasing.

Quote on the aliasing rules from the C standard:

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

  • the declared type of the object,

  • a qualified version of the declared type of the object,

  • a type that is the signed or unsigned type corresponding to the declared type of the object,

  • a type that is the signed or unsigned type corresponding to a qualified version of the declared 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.

Does this mean that if I declare a variable of a type, say,

struct struct1 {
    int a;
};

/* ... */

/* an object. The declared type of the object is struct struct1 */
struct struct1 test;

And declare another of a type, say,

struct struct2 { /* an aggregate or union type that includes... */
    int a;
    struct struct1 test; /* ...one of the aforementioned types among its members:
                            (the declared type of the object) */
};
/* ... */
struct struct2 test2;

Are they technically supposed to alias, as per the quote above? That seems very wrong.
What am I missing?

Community
  • 1
  • 1
  • 1
    Related to [Aliasing Arrays through structs](http://stackoverflow.com/q/27908626/1708801), perhaps duplicate – Shafik Yaghmour Jan 15 '15 at 18:01
  • 2
    struct2 can alias struct1, but that doesn't mean you can interpret object of type struct1 as it were an object of type struct2. You can however use type struct2 and use it's member test to access struct1. – 2501 Jan 15 '15 at 18:21

0 Answers0