www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1993/10/05/12:34:10

Date: Tue, 5 Oct 93 11:46:17 -0400
From: DJ Delorie <dj AT ctron DOT com>
To: johnson AT cdsmn DOT mn DOT org
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: how do you pack structs?

> Is it possible to pack a struct, so that it uses the least amount of memory
> required for the data.

> example:
> struct {
>     uchar reg;
>     uchar *scr;
>     uchar r,g,b;
> } RGB;

> Normally sizeof(RGB)==12.  Is it possible to tell the compiler NOT to pad out
> structs to multiple-of-4 sizes.  I understand that in the above example,
> swapping reg and scr would result in a sizeof()==8 (which is what I want),
> but when you are working with file headers that are not of your own design,
> you can not simply reorder your struct.

As of gcc 2.4.1 you can use the "__attribute__((packed))" construct to
cause an object to pack (byte boundaries).  Consult the gcc
documentation for specifics on using this.

DJ


- Raw text -


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