This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/77388] Reference to a packed structure member
- From: "rguenther at suse dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 26 Aug 2016 10:59:19 +0000
- Subject: [Bug c++/77388] Reference to a packed structure member
- Auto-submitted: auto-generated
- References: <bug-77388-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77388
--- Comment #4 from rguenther at suse dot de <rguenther at suse dot de> ---
On Fri, 26 Aug 2016, andre.simoesdiasvieira at arm dot com wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77388
>
> --- Comment #3 from Andre Vieira <andre.simoesdiasvieira at arm dot com> ---
> Thank you Richard!
>
> I have a follow up question. Why is this only a problem when passing by
> reference and not when passing a pointer?
>
> So say:
> #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));
> }
>
> Wouldn't there still be an alignment mismatch between A::c and s->c?
Yes, this still invokes undefined behavior. You just don't get bitten
by the C++ FE choosing to use a temporary for the const reference.
I suppose using a non-const reference would "work" as well.
Note the above is also not diagnosed.