www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/14/01:25:48

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199606140523.AA095329810@relay1.geis.com>
Date: Fri, 14 Jun 96 04:48:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: How are bool's allocated?

Reply to message 4458557    from CALVID AT GOYA DOT I on 06/13/96 10:20AM


>In DJGPP, how is memory allocated for bool variables?  Is just
>1 bit allocated, or a whole byte?  If you do sizeof(bool), it
>returns 1, but maybe that's just a limitation of sizeof().
>I'm planning on using a large number of bool's in a structure,
>and I was wondering if it would be more efficient to declare
>them all as bool's, or to use unsigned characters and use
>bitwise ops to access each bit that way.  Thanks for the info.

There is no bool type in DJGPP.  It is non-ANSI, leaving it up to
programmers to determine what type suits them best.  There is
a 'bool.h' in the 'lang/cxx' directory, but that simply defines bool
as an int, which is hardly optimal for space purposes.  How did
you manage to get sizeof(bool) to work?  Are you perchance
using C++ instead of C?

I personally use 'typedef unsigned char bool' and it works like a
charm.  However, I have never had a need to manipulate whole
arrays of boolean variables.  IMHO the most memory-efficient
way to do it is with bitwise ops on an unsigned long (the most
natural word size for a 32-bit compiler).

If all of your boolean variables are closely related, you could
define them as a single unsigned int or long and check bits.  However,
if they are fairly unrelated, it might be just as easy to simply define
bool as the smallest type size you can find and leave it at that.
Remember that you have access to large amounts of virtual
memory under DJGPP, so you really shouldn't have to worry about
space unless you are writing one whopper of a program.

John

- Raw text -


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