Re: Off-topic

Richard B. Johnson (
Wed, 5 Mar 1997 15:21:04 -0500 (EST)

On Wed, 5 Mar 1997, Mike Kilburn wrote:

> On Tue, 4 Mar 1997, Richard B. Johnson wrote:
> > Anyone know how to get gcc to NOT pad structs?
> > __atttribute__((aligned (1))) does NOT work. It insists upon aligning
> > long-words to long-words boundaries. The problem is that a certain data
> > element ___specified___ by someone else, MUST exist in a light-pipe packet
> > EXACTLY where specified!
> >
> As far as I can tell gcc has a bug in this regard (someone please correct
> if I am wrong). The only way is to use the global directive on the gcc
> command line and this packs *all* structures. The docs warn against this
> due to some libc routines requiring a particular structure alignment. This
> seems very bad to me unless a standard exist for how things are packed. In
> the end I had to pack and unpack manually. This was a huge dos program
> that I ported to Linux and it wrote/read structures to/from disk,
> apparently Borland C 4.0 always packed them and I had to read/write old
> data files. If you can figure out how to get __attribute__ ((packed)) (or
> equivalent) to work please let me know.
Yes. I discovered the -fpack-struct command-line switch early on. However,
as you note, it packs __EVERYTHING__ and therefore things that use the
FILE structure, etc., don't work!

However, thanks to help from others, the following works:

#include <stdio.h>

struct foo {
short int one __attribute__((packed));
long int two __attribute__((packed));
} FOO;

printf("sizeof FOO = %u\n", sizeof(FOO));
return 0;


This returns a length of 6 which is what I want. Bare bones, left to
gcc's whim, it would return a value of 8 as it quietly aligns the
longword on a longword boundary. This IS what most people want. However,
when you are dealing with a serial data-stream, you have to take what
you get. Of course, you could pretend that the serial data stream was
a string of bytes, then initialize various kinds of pointers to
point to various data-elements. That is kind of hokey.

Dick Johnson
Richard B. Johnson
Project Engineer
Analogic Corporation
Voice : (508) 977-3000 ext. 3754
Fax : (508) 532-6097
Modem : (508) 977-6870
Ftp :
Email :,
Penguin : Linux version 2.1.28 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.