[Bug c/51628] __attribute__((packed)) is unsafe in some cases
sven.koehler at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Apr 12 09:57:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51628
--- Comment #49 from Sven <sven.koehler at gmail dot com> ---
(In reply to W.H. Ding from comment #47)
> Hi, everyone
>
> I wonder if this issue has to do with the bug-like problem I encountered
> when accessing an unaligned stand-alone global variable (rather than a
> member of a packed struct). A test case is as follows:
>
> char g_c = 'x';
> int g_d __attribute__((aligned(1))) = 13;
>
> int main(void)
> {
> g_c = 'z';
> //================
> g_d = 33; // Crash on, in my case, ARM Cortex-M0
> //================
> return 0;
> }
This doesn't work. The aligned attribute is for providing additional alignment
hints. The GCC documentation clearly states, that aligned can increase the
alignment. So g_d is still 4 byte aligned, and correctly so.
Also, you cannot use the packed attribute (which reduces the alignment to 1)
for simple types. You can only use it for structs.
Try a packed struct that contains a single int. That will work.
More information about the Gcc-bugs
mailing list