This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: How to stop gcc padding structs???
- To: Grant Edwards <grante at visi dot com>
- Subject: Re: How to stop gcc padding structs???
- From: Richard Earnshaw <rearnsha at arm dot com>
- Date: Mon, 29 Jan 2001 10:26:40 +0000
- cc: gcc at gcc dot gnu dot org, Richard dot Earnshaw at arm dot com
- Organization: ARM Ltd.
- Reply-To: rearnsha at arm dot com
>
> I'm again fighting with gcc trying (and failing) to get it to
> stop putting padding bytes into structs. Has anybody figured
> out how to prevent gcc from padding structs?
>
> I ran into this problem before and gave up, finally having to
> use hand-calculated constants instead of "sizeof (struct foo)"
> in numerous places. For example, it's impossible to define an
> Ethernet header structure that ends up having a size of 14
> bytes!
Relying on sizeof to do this is *very* non-portable.
>
> In the following example, gcc insists that each of the "high"
> structs occupies four bytes despite my putting a "packed"
> attribute evryplace that doesn't generate a syntax warning.
>
> $ arm-elf-gcc -v
> Reading specs from /usr/local/lib/gcc-lib/arm-elf/2.95.2/specs
> gcc version 2.95.2 19991024 (release)
>
>
> typedef struct
> {
> volatile unsigned char data __attribute__((packed));
> volatile unsigned char _xxx __attribute__((packed));
> } high __attribute((packed));
Try:
typedef struct
{
volatile unsigned char data;
volatile unsigned char _xxx;
} __attribute((packed)) high;
etc.
The following also works:
struct foo
{
volatile unsigned char data;
volatile unsigned char _xxx;
} __attribute((packed));
typedef struct foo high;
IMO it's a long-standing bug in gcc's parser grammar that your example
doesn't pack things as expected but doesn't generate a diagnostic either.
R.