[Bug c/82105] unexpected padding in a struct
egallager at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Tue Sep 5 15:29:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82105
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |egallager at gcc dot gnu.org
--- Comment #5 from Eric Gallager <egallager at gcc dot gnu.org> ---
I combined these all into a single testcase:
$ cat 82105.c
typedef struct {
unsigned int x:16;
unsigned int y:17;
unsigned short z0; /* -Wpadded 1 */
} XXX; /* -Wpadded 2 */
typedef struct __attribute__((packed,aligned(4))) {
unsigned int x:16;
unsigned int y:17;
unsigned short z1; /* -Wpadded 3 */
} XXY; /* -Wpadded 4 */
typedef struct {
unsigned int x:1;
unsigned int y:1;
unsigned short z2; /* -Wpadded 5 */
} XYX; /* silent, good */
typedef struct {
unsigned int x:16;
unsigned int y:16;
unsigned short z3; /* silent, good */
} XYY; /* -Wpadded 6 */
$ /usr/local/bin/gcc -c -Wall -Wextra -pedantic -Wpadded -Wpacked -O2 82105.c
82105.c:4:17: warning: padding struct to align ‘z0’ [-Wpadded]
unsigned short z0; /* -Wpadded 1 */
^~
82105.c:5:1: warning: padding struct size to alignment boundary [-Wpadded]
} XXX; /* -Wpadded 2 */
^
82105.c:10:20: warning: padding struct to align ‘z1’ [-Wpadded]
unsigned short z1; /* -Wpadded 3 */
^~
82105.c:11:1: warning: padding struct size to alignment boundary [-Wpadded]
} XXY; /* -Wpadded 4 */
^
82105.c:16:17: warning: padding struct to align ‘z2’ [-Wpadded]
unsigned short z2; /* -Wpadded 5 */
^~
82105.c:23:1: warning: padding struct size to alignment boundary [-Wpadded]
} XYY; /* -Wpadded 6 */
^
$
(In reply to Richard Biener from comment #1)
> This is how bitfields work. You can use
>
> typedef struct __attribute__((packed,aligned(4))) {
> unsigned int x:16;
> unsigned int y:17;
> unsigned short z;
> } XXX;
>
> to get what you want.
It still prints -Wpadded warnings despite the attributes though.
More information about the Gcc-bugs
mailing list