Topic: Space required by a bool


Author: "Gary Powell" <gary.powell@sierra.com>
Date: 1997/11/25
Raw View
<lots of cuts.>
I would like to know what size this would have:

struct record {
 bool flag1;
 bool flag2;
 bool flag3;
};

bool array[10];

sizeof record?  sizeof array?

It would be nice/intuitive? if the compiler would pack these booleans
into single bytes/ints.

<signature cut>

Since bool is machine/implementation dependent for this data structure to
be read in correctly, I'll have to write more typedefs. Just like I do for
ints.

i.e.

typedef struct {
 bool b;
 ] bool32;
typedef struct {
 char padding[2];
 bool b;
 } bool16; // for reading 16 bit bools written by 32 bit bool machines.

typedef struct {
 char padding[3];
 bool b;
 } bool8;

Of course since 64bit machines are available maybe I should start with a
bool64. Or maybe to save space I'll just use a non padded bool8 and convert
it up on read/write for all other architectures.  Of course it could be
just one bit. Which if I had a really good compiler...(hah!) could optimize
 bool bArray[32] = { .....initialize somehow.};
 for (i = 0; i < 32; ++i) {
  if ( bArray[i] == true) {
   DoSomething();
   break;
  }
 }

 to
 if (*((long *) bArray) DoSomething(); // Where sizeof(long) == sizeof
(bArray)

The long and the short of it is binary compatiblity just isn't there yet.
Mostly for reasons of processor speed vs data compatibility. I only posed
the question since bool was a new type, that maybe just maybe it would be a
fixed size.

---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/11/25
Raw View
On 24 Nov 97 15:32:15 GMT, Per Erik Stendahl
<berrs@cyberdude.comyouknowwhat> wrote:

>I would like to know what size this would have:
>
>struct record {
> bool flag1;
> bool flag2;
> bool flag3;
>};
>
>bool array[10];
>
>sizeof record?  sizeof array?

The actual size depends on the size the implementation has selected
for type bool, and on any additional alignment requirements. The
sizeof operator is a compile-time constant that lets you know and use
the size of the record or array in a portable way.

Presumably the compiler implementor has chosen a size for bool that is
appropriate given various platform constraints. Ordinarily letting the
compiler pick the size and alignment provides the best overall
solution.

>It would be nice/intuitive? if the compiler would pack these booleans
>into single bytes/ints.

Sometimes you care about how much space is actually used, as when you
have huge arrays. You can force the compiler to do packing by using
bitfields, since you are guaranteed that bool fits in a one-bit
bitfield . Examples:

struct record1 { // the flags must be packed into a single byte
 bool flag1 : 1;
 bool flag2 : 1;
 bool flag3 : 1;
};

#include <limits.h>
struct record2 { // each flag must occupy exactly one byte
 bool flag1 : CHAR_BIT;
 bool flag2 : CHAR_BIT;
 bool flag3 : CHAR_BIT;
};

The size of each struct will still depend on platform alignment
requirements. Some platforms support 1-byte or 3-byte structs, but
many require that struct size be a multiple of 2 or 4.

---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Paul Black <paul.black@vf.vodafone.co.uk>
Date: 1997/11/24
Raw View
"Gary Powell" <gary.powell@sierra.com> wrote:
> Does the standard guarantee the amount of space that a bool will occupy in
> a POD struct? Or is it implementation dependent? (like int)

It's implementation defined.

> i.e..
> struct {
>         bool    var1;
> } mystruct;
>
> given that alignment is set to 1.
>
> what is sizeof(mystruct) ? (I can find out on this release of my compiler
> but if it could be changed in a future release I'd rather just store
> something I know will be a given size.)

There is no type that you can depend on being a certain size.
If it's because you're intending to create a struct that maps to a file
format
then you're better off writing functions to do the conversions for you.
It's
more portable.

Paul
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Per Erik Stendahl <berrs@cyberdude.comyouknowwhat>
Date: 1997/11/24
Raw View
Gary Powell wrote:
>
> Does the standard guarantee the amount of space that a bool will occupy in
> a POD struct? Or is it implementation dependent? (like int)
>
> i.e..
> struct {
>         bool    var1;
> } mystruct;
>
> given that alignment is set to 1.
>
> what is sizeof(mystruct) ? (I can find out on this release of my compiler
> but if it could be changed in a future release I'd rather just store
> something I know will be a given size.)

I would like to know what size this would have:

struct record {
 bool flag1;
 bool flag2;
 bool flag3;
};

bool array[10];

sizeof record?  sizeof array?

It would be nice/intuitive? if the compiler would pack these booleans
into single bytes/ints.

Regards,
Per Erik Stendahl
Swedish Insitute of Computer Science
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Gary Powell" <gary.powell@sierra.com>
Date: 1997/11/21
Raw View
Does the standard guarantee the amount of space that a bool will occupy in
a POD struct? Or is it implementation dependent? (like int)

i.e..
struct {
 bool var1;
} mystruct;

given that alignment is set to 1.

what is sizeof(mystruct) ? (I can find out on this release of my compiler
but if it could be changed in a future release I'd rather just store
something I know will be a given size.)

   -Gary-
--
_____________________
gary.powell@.sierra.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]