delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/21/18:33:11

From: gk_2345 AT my-deja DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: Re: structure size rounded to int size?
Date: Thu, 21 Oct 1999 20:21:48 GMT
Organization: Deja.com - Before you buy.
Lines: 39
Message-ID: <7unsko$j91$1@nnrp1.deja.com>
References: <380F12E1 DOT 74D9DE4 AT umh DOT ac DOT be>
NNTP-Posting-Host: 209.138.153.30
X-Article-Creation-Date: Thu Oct 21 20:21:48 1999 GMT
X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
X-Http-Proxy: 1.0 x41.deja.com:80 (Squid/1.1.22) for client 209.138.153.30
X-MyDeja-Info: XMYDJUIDgk_2345
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <380F12E1 DOT 74D9DE4 AT umh DOT ac DOT be>,
  Fabrice LETE <et980139 AT umh DOT ac DOT be> wrote:
> If I have:
>
> 	typedef struct FOO {
> 	   short a; long b;
> 	};
>
> sizeof(FOO) give me 8...
>
> What can I do to avoid the problem (I guess it's a switch to use with
> the compiler...)?

errrr... sort of. There are macros you can embed in the struct
definition to get around this.

The 'problem' is that gcc by default aligns the members of structs along
whatever alignment boundaries are handy for a given CPU. This means that
the beginning of b isn't always the byte immediately following the end
of a.

re-write the above as:
 	typedef struct FOO {
 	   short a __attribute__((packed));
           long  b __attribute__((packed));
 	};

and FOO will now be 6 bytes long.

Consult the gcc docs, and DJGPP FAQ for more details.

NOTE: packed struct take longer to access than aligned structs, so only
do this if there is actually a reason to.

Good luck.


Sent via Deja.com http://www.deja.com/
Before you buy.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019