* The pack attribute
@ 2002-09-10 10:29 John Kwan
2002-09-10 11:37 ` Richard Henderson
0 siblings, 1 reply; 2+ messages in thread
From: John Kwan @ 2002-09-10 10:29 UTC (permalink / raw)
To: gcc
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: The pack attribute
2002-09-10 10:29 The pack attribute John Kwan
@ 2002-09-10 11:37 ` Richard Henderson
0 siblings, 0 replies; 2+ messages in thread
From: Richard Henderson @ 2002-09-10 11:37 UTC (permalink / raw)
To: John Kwan; +Cc: gcc
On Tue, Sep 10, 2002 at 10:23:43AM -0700, John Kwan wrote:
> // case 1
> typedef struct {
> unsigned char byte1;
> int i;
> unsigned char byte2;
> } test_struct_1 __attribute__((packed));
Well, you see, the packed attribute here does not apply
to the structure. It applies to the typename, which is
created after the structure is layed out.
Write it like
typedef struct __attribute__((packed)) {
...
} test_struct_1;
or
typedef struct {
...
} __attribute__((packed)) test_struct_1;
We should probably generate a warning for your example; please
file a gnats bug report so this does not get forgotten.
r~
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2002-09-10 18:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-10 10:29 The pack attribute John Kwan
2002-09-10 11:37 ` Richard Henderson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).