This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/14786] New: Struct misaligned containing unsigned char bit-field
- From: "ryanma at cpsc dot ucalgary dot ca" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 30 Mar 2004 19:13:06 -0000
- Subject: [Bug c/14786] New: Struct misaligned containing unsigned char bit-field
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Description:
According to RS/6000 Alignment Rules, structs containing
bit fields are 4-byte (word) aligned. The following testcase
illustrates that this rule is violated, when Substruct B is not
word aligned.
Testcase:
extern int printf(const char *, ...);
typedef unsigned char UCHAR;
typedef struct {
UCHAR a : 1 ;
UCHAR b : 1 ;
UCHAR c : 1 ;
UCHAR d : 1 ;
UCHAR e : 1 ;
UCHAR f : 1 ;
UCHAR g : 1 ;
UCHAR h : 1 ;
UCHAR i : 1 ;
UCHAR J : 1 ;
UCHAR K : 6 ;
} SubStruct ;
typedef struct {
UCHAR A[5];
SubStruct B ;
UCHAR C ;
UCHAR D[4];
} Struct ;
int main() {
Struct data;
printf("sizeof(SubStruct) =%d\n",sizeof(SubStruct));
printf("sizeof(Struct) =%d\n",sizeof(Struct));
printf(" offset of B =%d\n",
((unsigned int)&(data.B)-(unsigned int)&data));
printf(" offset of C =%d\n",
((unsigned int)&(data.C)-(unsigned int)&data));
printf(" offset of D =%d\n",
((unsigned int)&(data.D)-(unsigned int)&data));
return 0;
}
Actual Output:
$ gcc t.c
$ ./a.out
Address of Struct =2ff22a20
sizeof(SubStruct) =2
sizeof(Struct) =12
offset of B =5
offset of C =7
offset of D =8
$
Expected Output:
$ gcc t.c
$ ./a.out
Address of Struct =2ff22a20
sizeof(SubStruct) =2
sizeof(Struct) =20
offset of B =8
offset of C =12
offset of D =13
GCC Version:
$ /home/ryan/gcc-3.3.2/aix51/bin/gcc -v
Reading specs from /home/ryan/gcc-3.3.2/aix51/bin/../lib/gcc-lib/powerpc-ibm-
aix5.1.0.0/3.3.
2/specs
Configured with: ../gcc-3.3.2/configure --disable-nls
Thread model: aix
gcc version 3.3.2
Machine Type:
Model: 7044 -170
Processor: 1 x pwr3
Processor Speed: 400 MHz
Memory: 1 GB
AIX Level: AIX 5.1 ML 5
Hardware Arch: 64 bit
Kernel Arch: 32 bit
--
Summary: Struct misaligned containing unsigned char bit-field
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ryanma at cpsc dot ucalgary dot ca
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: powerpc-ibm-aix5.1.0.0
GCC host triplet: powerpc-ibm-aix5.1.0.0
GCC target triplet: powerpc-ibm-aix5.1.0.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14786