http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47273

--- Comment #4 from Waldemar Valdas Bancewicz <waldemarbancewicz at ruggedcom 
dot com> 2011-01-14 14:04:19 UTC ---
The reason pointers and references are treated differently:

Consider the following code fragment:

struct s {
    char a;
    int b;
    int* GetValp() { return &b; }
    //int& GetValr() { return b; }
} __attribute__((packed));

The GetValp code will compile to something like:

if &b is aligned
  return address of b
else
 do aligned read before pointer and after pointer, bitshift and mask each read
and merge to get data

However, GetValr, by returning a reference, always assumes that the reference
is aligned. Since we are returning a reference to a packed structure member,
the compiler will not allow this. Therefore, if the user is absolutely sure the
reference is aligned, using references gives a slight performance boost over
pointers. However, this performance gain only happens on some architectures.
Hence my request to add a g++ compiler option to disable this optimization.

Reply via email to