www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/05/03/23:25:17

Date: Wed, 03 May 1995 21:29:27 -0500 (CDT)
From: Aaron Ucko <UCKO AT VAX1 DOT ROCKHURST DOT EDU>
Subject: Re: [Q] Structs and spacing of data
To: eheft AT valhalla DOT cs DOT wright DOT edu
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Organization: Rockhurst College; Kansas City, MO

>   I've got a stuct like 
>
>typedef struct {
>   short id;
>   char  fc[3];
>   short pos;
>   ...
>} trREC,*tpREC;
>
>when I go to read the struct from a binary file the struct
>appears to have padded a space between then end of fc and
>start of pos. Is there a pragma to make sure the struct uses
>the minimum amout of space? How much of a speed penalty will
>I pay in the rest of the code (hav'ing non-word boundries)?
>Would it be better skip the pragma and just to read the
>fields one at a time?

There isn't a pragma _per se_, but there is the packed attribute; just
do something like
typedef struct {
	short id;
	char fc[3] __attribute__ ((packed));
	short pos __attribute__ ((packed));
	...
} trREC, *tpREC;

[The double parentheses are to allow multiple attributes; if only a single
set were used it wouldn't be nearly as easy to keep attribute information
from inferior compilers.]  I don't know what the size-speed tradeoff is,
but I get the impression that the packed attribute is mainly used for things
like exact matching of hardware data structures.  Try some experiments and
post if you find any major differences.

--- Aaron Ucko (ucko AT vax1 DOT rockhurst DOT edu; finger for PGP public key) | httyp!
Geek code 2.1 [for explanation, finger hayden@ | `God's Laws' (Rudy Rucker,  
vax1.mankato.msus.edu]: GCS/M/S d(-) H s g+ p? \ _Master of Space and Time_):
!au a17 w+ v+(++) C++(+++)>++++ UL++(S+)>++++	\ 1) Be clean. 2) Follow Gary.
P+(++) L++ 3(-) E- N+(++) K- W--(---) M-@ V-(--) \ 3) Teach God's Laws.
po-(--) Y+ t(+) !5 j(++) R(-) G' tv--(-) b+++	  +---------------------------
!D(--) B--(---) e>++++(*) u++(@) h!() f(+) r-(--)>+++ n+(-) y?

- Raw text -


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