https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77388
Bug ID: 77388 Summary: Reference to a packed structure member Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: andre.simoesdiasvieira at arm dot com Target Milestone: --- As initially reported by Michal on https://answers.launchpad.net/gcc-arm-embedded/+question/345145 gcc seems to be showing some weird behavior when it comes to passing a reference to a member in a packed structure. I was able to reduce the testcase presented in that launchpad ticket to the following program: $cat t.cpp: #define PACKED __attribute__ ((packed)) #define TYPE_C short typedef struct { TYPE_C c; } PACKED test_struct; class A { const TYPE_C &c; public: A (const TYPE_C & _c) : c(_c) {}; }; class B { public: B(); A foo (); private: test_struct * s; }; A B::foo () { return A (s->c); } Compiling this with $arm-none-eabi-g++ -mcpu=cortex-m7 -mthumb -S -O1 t.cpp -fdump-tree-optimized Will yield the following dump: ;; Function A B::foo() (_ZN1B3fooEv, funcdef_no=3, decl_uid=4607, cgraph_uid=3, symbol_order=3) A B::foo() (struct B * const this) { const short int D.4636; struct A D.4650; <bb 2>: MEM[(struct A *)&D.4650] = &D.4636; D.4636 ={v} {CLOBBER}; return D.4650; } As you can see, it will not load the struct's field. Changing the 'TYPE_C' define to 'char' will yield the following dump: ;; Function A B::foo() (_ZN1B3fooEv, funcdef_no=3, decl_uid=4607, cgraph_uid=3, symbol_order=3) A B::foo() (struct B * const this) { struct A D.4649; struct test_struct * _1; char * _2; <bb 2>: _1 = this_4(D)->s; _2 = &_1->c; MEM[(struct A *)&D.4649] = _2; return D.4649; } Now when the type is 'char' it seems to be able to get the fields address. Can anyone shine some light on this for me? Is referencing a packed structure's member that is not guaranteed to be aligned (so not char) undefined behavior?