8

I was wondering if bools in C++ are actually 1-bit variables. I am working on a PMM for my kernel and using (maybe multidimensional) bool-arrays would be quiet nice. But i don't want to waste space if a bool in C++ is 8 bit long...

EDIT: Is a bool[8] then 1 Byte long? Or 8 Bytes? Could i maybe declare something like bool bByte[8] __attribute__((packed)); when using gcc? And as i said: I am coding a kernel. So i can't include the standard librarys.

DevNoteHQ
  • 303
  • 1
  • 3
  • 15
  • A `bool` can be 1 byte or more, depending on the implementation. See [fundamental types](http://en.cppreference.com/w/cpp/language/types). – zett42 May 21 '17 at 20:25
  • it's 1byte (8 bits), Use bitfields or manually write code to access bits in memory buffer. – Pavel P May 21 '17 at 20:45
  • 1
    As for your [recent question](http://stackoverflow.com/questions/44102439/c-trying-to-make-a-tostring-function?noredirect=1#comment75225575_44102439), you're so off with what you want to achieve it seems. I'll give you the chance to contact me privately at g-makulik t-online.de to discuss that via eMail and getting some serious and founded advice. Otherwise taking on to read some text book from [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should serve you equally well. – πάντα ῥεῖ May 21 '17 at 23:48
  • @Pavel: Please provide a reference to the standard where 1) A byte is guaranteed to be 8 bits and 2) `bool` is guaranteed to be 1 byte. – too honest for this site May 22 '17 at 02:54
  • @Olaf I write code, not standards, I care more for what I see in reality. Those who work with obscure environments where `sizeof(bool) != 1` or `CHAR_BIT != 8` don't ask these types of questions. Question was mostly if a bool variable takes 1 bit or not. – Pavel P May 22 '17 at 03:12
  • @Pavel: If you do not program standard **compliant**, you run into trouble even on x86, ARM, etc. platforms. C++ (and C, btw.) is not a language for trial&error. Ignoring the standard is nothing one should be proud of as your comment implies. Said that: your statement was unnecessary and spreads two relevant missconceptions **without any need**. Note this is read by beginners who take this seriously! Finally: Just because you don't know such targets does not make them "obscure"; they are fully compliant. Widening one's horizon never is a bad idea. – too honest for this site May 22 '17 at 12:09
  • @Olaf so, Olaf, what are those targets? Short and simple – Pavel P May 22 '17 at 14:39
  • @Pavel: 16 and 32 bit DSPs for instance. FPGA softcores for instance. You'd be surprised how wide spread such architectures are! As an expert you certainly will find specific products. – too honest for this site May 22 '17 at 14:41
  • I do know how widespread are all kids of architectures, I've seen all kids of stuff in last 15 years. However, are you 100% sure that sizeof(bool) on these isn't 1? Also, there are many DSPs and FPGAs, anything specific that has this exact behavior, where you personally saw that. – Pavel P May 22 '17 at 14:45

3 Answers3

7

No there's no such thing like a 1 bit variable.

The smallest unit that can be addressed in c++ is a unsigned char.

Is a bool[8] then 1 Byte long?

No.

Or 8 Bytes?

Not necessarily. Depends on the target machines number of bits taken for a unsigned char.


But i don't want to waste space if a bool in C++ is 8 bit long...

You can avoid wasting space when dealing with bits using std::bitset, or boost::dynamic_bitset if you need a dynamic sizing.


As pointed out by @zett42 in their comment you can also address single bits with a bitfield struct (but for reasons of cache alignement this will probably use even more space):

struct S {
    // will usually occupy 4 bytes:
    unsigned b1 : 1, 
             b2 : 1,
             b3 : 1;
};
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
3

A bool uses at least one (and maybe more) byte of storage, so yes, at least 8 bits.

A vector<bool>, however, normally stores a bool in only one bit, with some cleverness in the form of proxy iterators and such to (mostly) imitate access to actual bool objects, even though that's not what they store. The original C++ standard required this. More recent ones have relaxed the requirements to allow a vector<bool> to actually be what you'd normally expect (i.e., just a bunch of bool objects). Despite the relaxed requirements, however, a fair number of implementations continue to store them in packed form in a vector<bool>.

Note, however, that the same is not true of other container types--for example, a list<bool> or deque<bool> cannot use a bit-packed representation.

Also note that due to the requirement for a proxy iterator (and such) a vector<bool> that uses a bit-packed representation for storage can't meet the requirements imposed on normal containers, so you need to be careful in what you expect from them.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • http://stackoverflow.com/questions/44101342/c-is-bool-a-1-bit-variable/44101466#comment75223172_44101465 – πάντα ῥεῖ May 21 '17 at 20:30
  • @πάνταῥεῖ: Why, exactly, do you think it's useful to point me at a wrong answer? – Jerry Coffin May 21 '17 at 20:33
  • I pointed you to my comment there actually. And your answer is bad for the same reasons. – πάντα ῥεῖ May 21 '17 at 20:35
  • @πάνταῥεῖ: Your comment is almost equally wrong. While it's certainly true that some people take the view that "vector` is bad, this view is overly simplistic at best. Most of those are based on the fact that `vector` (of other types) is a container, while `vector` is not. This *can* violate expectations, but in this case the OP seems to desire exactly what it provides. Your unjustified bias against `vector` does not make every answer that dares to mention it "bad". – Jerry Coffin May 21 '17 at 20:40
  • @Jerry Obviously not at all ;-) – πάντα ῥεῖ May 21 '17 at 20:45
  • 1
    @MathiLpHD: coding a kernel doesn't mean you can't use `std::` function at all. Standard containers (other than `std::array`) can use dynamic allocation, so you'd need to provide an allocator that worked in kernel mode, but not exactly rocket science. – Jerry Coffin May 21 '17 at 20:48
  • Well lets say it this way: I can't use any std functions without having to provide a lot of stuff and without getting a lot of overhead. I could also write a function myself that manipulates bits using << operators. That would be the faster method. But i would prefer to not use functions at all to minimize overhead that decreases performance. – DevNoteHQ May 21 '17 at 20:53
  • @MathiLpHD: What "lot of stuff" are you referring to? Have you actually attempted to measure the overhead you're talking about? What makes you think that code you write would be a lot faster than code written by, say, Marshall Clow or Howard Hinnant? They're both actually quite good coders (and so are most other standard library maintainers, from what I've seen). – Jerry Coffin May 21 '17 at 21:04
-2

The smallest unit of addressable memory is a char. A bool[N] or std::array<bool, N> will use as much space as a char[N] or std::array<char, N>.

It is permitted by the standard (although not required) that implementations of std::vector<bool> may be specialized to pack bits together.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 2
    Not true: _"bool - type, capable of holding one of the two values: true or false. The value of sizeof(bool) is implementation defined and might differ from 1."_ source: http://en.cppreference.com/w/cpp/language/types – Richard Critten May 21 '17 at 20:28
  • @RichardCritten Strictly true but not in the way OP expects. If it differs, it can only differ by being greater than 1. – ephemient May 21 '17 at 20:29
  • `std::vector` is considered bad for various (and valid) reasons. – πάντα ῥεῖ May 21 '17 at 20:29
  • @πάνταῥεῖ That is true. Unfortunately if `N` is not known at compile-time it is not possible to use `std::bitset` (unlike `std::vector` or `bool[N]`), so it depends on the use case. – ephemient May 21 '17 at 20:36
  • @ephemient Well, there's still [`boost::dynamic_bitset`](http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html). – πάντα ῥεῖ May 21 '17 at 20:58