[Bug c/33823] New: bitfields on packed struct aligns by a few bits if the types differ
alexandre dot nunes at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sat Oct 20 01:47:00 GMT 2007
I have two structs (the unions are there for the sake of testing, it still
behaves the same without them):
#define ATTRIBUTE_PACKED_STRUCT __attribute__((gcc_struct,packed))
#include <stdio.h>
#include <stdint.h>
typedef union ATTRIBUTE_PACKED_STRUCT
{
struct ATTRIBUTE_PACKED_STRUCT {
uint8_t a:1,
b:7,
c:7;
uint16_t d:16;
uint8_t e:7;
uint8_t f:7;
uint16_t g:10;
uint16_t h:10;
uint16_t i:12;
uint8_t j:7;
uint8_t k:8;
uint16_t l:15;
};
unsigned char values[14];
} test_struct;
typedef union ATTRIBUTE_PACKED_STRUCT
{
struct ATTRIBUTE_PACKED_STRUCT {
uint32_t a:1,
b:7,
c:7;
uint32_t d:16;
uint32_t e:7;
uint32_t f:7;
uint32_t g:10;
uint32_t h:10;
uint32_t i:12;
uint32_t j:7;
uint32_t k:8;
uint32_t l:15;
};
unsigned char values[14];
} test_struct2;
These structs have the same effective size, i.e., removing them from the
unions, and having sizeof() applied to them yelds the 14 bytes size, but the
members dispostion differs, i.e. the 'e' member is a bit ahead in the first
struct (test_struct) than in the second.
I didn't expect the type change to force an alignment. It that's standard
behaviour, then I would like to propose a documentation request; The ms_struct
alignment is described quite well on gcc's page, but gcc's default one isn't.
If that's not expected behaviour, then what would be? In case it is, could
someone please point me to a documentation source, i.e. an ISO C pointer or
whatever reasoning is behind this, if any?
Thanks.
--
Summary: bitfields on packed struct aligns by a few bits if the
types differ
Product: gcc
Version: 4.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: alexandre dot nunes at gmail dot com
GCC build triplet: i486-linux-gnu
GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33823
More information about the Gcc-bugs
mailing list