www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/25/02:13:24

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199605250547.AA136263246@relay1.geis.com>
Date: Sat, 25 May 96 05:38:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Bit field problem. Help!

Reply to message 5691228    from JASONK AT DIRECT on 05/24/96  1:56AM


>struct
>  Life {
>           unsigned neighbours : 4;
>           unsigned alive : 1;
>           unsigned onTheList : 1;
>           unsigned : 2;
>          };
>
>It takes up 4 bytes instead of 1. I've tried using -mno-bit-align but
>it gives me an error like invalid option or something like that. I
>need it to take up only 1 byte. Any help greatly appreciated.

You are running into the fact that most 32-bit compilers (Borland being a
notable exception) pad structures to the nearest 4-byte boundary in
order to take advantage of the optimal speed of 4-byte move instructions.
Under GNU C the solution is to append "__attribute__((packed))" to your
structure definition, like so:

struct
  Life {
           unsigned neighbours : 4;
           unsigned alive : 1;
           unsigned onTheList : 1;
           unsigned : 2;
          } __attribute__((packed));

This will force the compiler to place the structure in 1 byte instead of 4.
You may also want to peruse some of the gcc docs that relate to
bitfields, because there's some interesting reading in there about how
bitfields are really treated by gcc.  As far as -mno-bit-align, that looks
like a valid option from what the docs say.  Are you sure that you're
typing it correctly?

John

- Raw text -


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