Alignement and/or sizeof trouble with struct!

Hello!

I have some trouble using qcc.

I have a struct looking like:

typedef struct {
unsigned int ID;
unsigned char Mode;
unsigned char Data[8]; /* 8 is only an exemple */
} MyType;

Having some trouble passing this struct to a driver of my own, I have looked
to sizeof(MyType) which equal 16. So, I first thought to 32 bits alignement
feature (4 bytes for ID, 1 byte + 3 dummy bytes for Mode and 8 bytes for
data.

But, I saw that &Data == (&Mode + 1) ) which means a 8bits alignement.

Do you have any idea?

Thanks

Vincent

“Vincent” <vincent.catros_NO_SPAM_@bigfoot.com> writes:

Hello!

I have some trouble using qcc.

I have a struct looking like:

typedef struct {
unsigned int ID;
unsigned char Mode;
unsigned char Data[8]; /* 8 is only an exemple */
} MyType;

Having some trouble passing this struct to a driver of my own, I have looked
to sizeof(MyType) which equal 16. So, I first thought to 32 bits alignement
feature (4 bytes for ID, 1 byte + 3 dummy bytes for Mode and 8 bytes for
data.

But, I saw that &Data == (&Mode + 1) ) which means a 8bits alignement.

Do you have any idea?

Thanks

Vincent

If you get 16 bytes for your structure, then it is not being packed.
Try explicitly packing it and see if the size changes to 13 bytes. It
should:

#define GCC_PACKED attribute ((packed))

typedef struct {
unsigned int ID;
unsigned char Mode;
unsigned char Data[8]; /* 8 is only an exemple */
} GCC_PACKED MyType;

\

Andrew Thomas, President, Cogent Real-Time Systems Inc.
2430 Meadowpine Boulevard, Suite 105, Mississauga, Ontario, Canada L5N 6S2
Email: andrew@cogent.ca WWW: http://www.cogent.ca

Vincent <vincent.catros_NO_SPAM_@bigfoot.com> wrote:

Hello!

I have some trouble using qcc.

I have a struct looking like:

typedef struct {
unsigned int ID;
unsigned char Mode;
unsigned char Data[8]; /* 8 is only an exemple */
} MyType;

Having some trouble passing this struct to a driver of my own, I have looked
to sizeof(MyType) which equal 16. So, I first thought to 32 bits alignement
feature (4 bytes for ID, 1 byte + 3 dummy bytes for Mode and 8 bytes for
data.

But, I saw that &Data == (&Mode + 1) ) which means a 8bits alignement.

Do you have any idea?

Since Data is “unsigned char”, it actually goes to:

4 bytes ID, 1 Bytes Mode, 8 bytes Data, 3 bytes pad

If Data is a “unsigned int”, then, since it have to start
on int alignment address, it goes to:

4 bytes ID, 1 Bytes Mode, 3 bytes pad, 4 byte Data

The same, if Data is a “short”, then, it goes to:

4 Bytes ID, 1 Bytes Mode, 1 byte pad, 2 byte Data

As others suggest, you can pack them if you want,
(but make sure you do the right thing. If you put
an int on unalignment address, some CPU will sigbus
you).

-xtang

Thanks

Vincent

Xiaodan Tang <xtang@qnx.com> writes:

Vincent <> vincent.catros_NO_SPAM_@bigfoot.com> > wrote via email:
Thank you for your answer, but I still miss something.
I would understand 3 padding bytes after “Mode” field in order to align
“Data” field on 32bits, but what 3 padding bytes at the end of the srtuct
are usefull for??

The last 3 bytes of padding are there to stretch the structure out to
an integer boundary so if you make an array of these structures, the
next one starts on an integer boundary. This is caused by the fact
that the first element is an int. If you change the first element to
char[4], then gcc will not pad the structure at all, since an array of
them will not cause any alignment issues.

Cheers,
Andrew


Andrew Thomas, President, Cogent Real-Time Systems Inc.
2430 Meadowpine Boulevard, Suite 105, Mississauga, Ontario, Canada L5N 6S2
Email: andrew@cogent.ca WWW: http://www.cogent.ca