Article View: aus.computers.linux
Article #54927Re: Help !! Byte alignmnet in gcc
From: Dimitri Maziuk
Date: Sat, 16 Aug 1997 00:00
Date: Sat, 16 Aug 1997 00:00
46 lines
2250 bytes
2250 bytes
Jason Nunn wrote:
>
> Chia-shyan Chou (cschou@csie.nctu.edu.tw) wrote:
> : I had write a structure as the following under linux:
>
> : unsigned short No1; // 2 byte
> : unsigned char No2; //1 byte
> : unsigned char No3; //1 byte
> : unsigned char No4; //1 byte
> : unsigned char No5; //1 byte
> : unsigned char No6; //1 byte
> : unsigned short No7; //2 byte
>
> : The length of this structure should be 9 bytes, however,
> : the sizeof function return 10.
>
> : How can I avoid this problem ? Does gcc compiler provide the conditional
> : switch to do byte alignment ?
>
> Your problem lies in how gcc aligns your data. Intel 32 bit machines
> running in Protected Mode, optimal alignment is 4 bytes. Therefore, bytes
> will be stored in a 4 byte space (i can't explain why your struct is 10
> bytes know?).. anyway, you need byte alignment.
Pure speculation: 1st 3 take one 32-bit word, next 3 take another and
then you need 2 more bytes for ushort#7. In other words, ushort#7
starts at word boundary and uchars 4,5 & 6 are padded with an extra
byte.
BTW you lose speed when you pack your bytes -- you gain space, of
course. Also, packing doesn't make your life easier if you intend
to e.g. read it with [f]getc() or recvfrom() -- they return int
which you may have to split into bytes; that's platform dependent,
too.
Just a thought...
--
Dimitri
if replying to a Usenet posting,
reply to emaziuk at curtin dot edu dot au
--------------------------------
The views expressed above (hereafter, views) are mine and ownership
remains with me. They are provided "as is" without expressed or
implied warranty of any kind, including, but not limited to, the
implied warranties of the suitability of the views for any purpose.
Technical Details
Message-ID:
<33F48044.6BF62B0@curtin.edu.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<5suenk$69a@news.csie.nctu.edu.tw> <5t0dut$552$1@news.wantree.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 102983,3d71c8d00623a26e X-Google-Attributes: gid102983,public From: Dimitri Maziuk <emaziuk@curtin.edu.au> Subject: Re: Help !! Byte alignmnet in gcc Date: 1997/08/16 Message-ID: <33F48044.6BF62B0@curtin.edu.au> X-Deja-AN: 268309090 References: <5suenk$69a@news.csie.nctu.edu.tw> <5t0dut$552$1@news.wantree.com.au> Organization: Legion of Static Discord Newsgroups: aus.computers.linux