[Asterisk-Dev] Re: ast_flags question

Tom Ivar Helbekkmo tih at eunetnorge.no
Sat Jan 8 14:19:49 MST 2005


"Kevin P. Fleming" <kpfleming at starnetworks.us> writes:

>> struct bitstruct {
>>         int b : 1;
>> };
>> struct bitstruct b;
>
> This does not save any bytes; this struct will still require 32 bits 
> (four bytes) for storage (see the "int" specified there?).

The "int" combines with the ":1" to mean that the field "b" is a 1 bit
integer, and only needs 1 bit of storage.  As it stands, it will
probably use 32 bits on a 32 bit machine, but if you add a field "int
c : 1;" to the struct, the whole struct will still only use 32 bits --
and so on, until you pass 32 such 1 bit variables.

A struct containing fewer than 32 of those 1 bit variables, and an int
that's used with a matching set of bit masks to isolate the needed
bits, can both use 32 bits of storage if the compiler chooses to do it
that way, but there are two clear advantages:

1) if (session.authenticated) looks better than
   if (session.flags & SESS_AUTHENTICATED)

2) the compiler doesn't have to pack the bit fields into a single int
   if it doesn't want to.  If it can generate more efficient code by
   letting them use more space, it's free to do so, within the
   constraints given to it at compile time, using compiler flags,
   pragmas, or whatever.

-tih
-- 
Tom Ivar Helbekkmo, Senior System Administrator, EUnet Norway Hosting
www.eunet.no  T +47-22092958 M +47-93013940 F +47-22092901 FWD 484145



More information about the asterisk-dev mailing list