Size of bitset 8 times too high

While trying to figure out why a serialized version of a class I made, took up way too much space on the harddrive. I discovered that a bitset takes up N amount of bytes. A bitset<24> for instance will, when serialized using Boost, create a file of 24 bytes plus a little bit of overhead, and if I use a bitset<32> instead, the size goes up by exactly 8 bytes.

This is consistent with using sizeof which returns 24 and 32, though I have googled it and gotten the impression that it should be no more than 16 bytes. But the N value used for a bitset<N> is documented as being the number of bits, not bytes, hence the size ought to be 8 times less than what it is, making it seem like it's using chars for each bit rather than collecting 8 bits into a char. Is this correct or is there something else mysterious going on?

If it helps, I'm using the C++11 standard and the latest Boost library to compile with.
Last edited on
This begs the question: how are you serializing your bitset?

[Edit: may be of interest to you.]
Last edited on
Using Boost as well, I just serialize every field of my class as per the tutorial. I've come across another such library, YAS, and when using that instead of Boost, not only does my files get much smaller, but sizeof returns what I would expect, like 4 for a bitset<24>. It leaves me wondering if the Boost library has overridden sizeof? Because if they haven't, then I can't come up with a logical explanation for this.

Okay, sizeof still produces the right results with Boost, but the file is still far larger than it should be. Leaves me wondering if Boost serializes each bit as a char, which is the only explanation I can think of right now, for why I get these results. And perhaps I used .size() instead of sizeof, which would then explain the wrong results for sizeof. Or it's a ghost in the machine.

Thank you! That link actually made it dawn on me. I tried setting a couple of bits in my bitset, writing it to a file, and then opening it in Notepad, and as it turns out Boost must save a bitset using a string, as the output from the to_string() function was inside the file. And that then perfectly explains the big size, since Boost does in fact use a char for every bit.
Last edited on
I've come across another such library, YAS

Topic archived. No new replies allowed.