This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/47273] References to unaligned packed structure members not allowed
- From: "waldemarbancewicz at ruggedcom dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 14 Jan 2011 14:04:21 +0000
- Subject: [Bug c/47273] References to unaligned packed structure members not allowed
- Auto-submitted: auto-generated
- References: <bug-47273-4@http.gcc.gnu.org/bugzilla/>
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.