This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
The pack attribute
- From: John Kwan <jkwan at mvista dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 10 Sep 2002 10:23:43 -0700
- Subject: The pack attribute
Consider the following program:
// *************** test.c
#include <stdio.h>
// case 1
typedef struct {
unsigned char byte1;
int i;
unsigned char byte2;
} test_struct_1 __attribute__((packed));
// case 2
struct test_struct_2 {
unsigned char byte1;
int i;
unsigned char byte2;test_struct_1 var1;
} __attribute__((packed));
test_struct_1 var1;
struct test_struct_2 var2;
main()
{
printf("SIZE of var1 = %d\n",sizeof(var1)); // prints 12 bytes
printf("SIZE of var2 = %d\n",sizeof(var2)); // prints 6 bytes
}
//**************** end of program
Intuitively I would expect case 1 and case 2 to product the same result
but they do not. The 3.1.1 gcc compiler prints 12 for case 1 and 6 for
case 2.
The gcc on line manual states:
"specifying this attribute for struct and union types is equivalent
to specifying the pack attribute on each of the structure or union
members"
This seems to imply that case 1 should also be 6 bytes in size.
However, 2lines later, the manual further states:
"You may only specify this attribute after the closing curly brace
on an enum
definition not in a typedef declaration ..."
This seems to imply that __attribute__((packed)) does not work on the
typedef
of an enum. No mention is made about structure/union typedefs.
My question is: Should case 1 and case 2 work differently? Or is this
feature
badly implemented.
Thanks.
JOhn W Kwan
MontaVista Software