1

I have difficulties finding out why my bitset takes up 29MB of memory when writing it to disk. I.e. the file that is written out is 29MB large. I write out the bitset in the following way:

#include <iostream>
#include <string>
#include <fstream>

int main() {
    std::bitset<29621645> a;
    std::ofstream out;
    out.open("myfile");
    out << a;
    out.close();

    return 0;
}

If I do the following, it shows that the bitset only uses 3702712 bytes:

std::cout << sizeof(a); // outputs 3702712

What am I missing here? It seams like it's using 1 byte for 1 bit. I expected the file to only be of size ~3.7 MB since I want actual 8 bits in a byte.

GoldenRetriever
  • 155
  • 3
  • 11
  • 3
    I'm surprised an object that large can even fit on the stack. At least in Windows I believe the stack size is just 1MB. – François Andrieux Apr 16 '21 at 19:26
  • 2
    << is formatted output. Make sure you didn't get a file full of ascii ones and zeros. – user4581301 Apr 16 '21 at 19:27
  • 2
    Yep. [Documentation reading for the win.](https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt2) – user4581301 Apr 16 '21 at 19:28
  • heap or stack is not my concern here. The code is to prove my point. – GoldenRetriever Apr 16 '21 at 19:30
  • 1
    What value where you expecting? How much is 3702712 times 8? – anastaciu Apr 16 '21 at 19:30
  • @FrançoisAndrieux, it crashes on Windows yes, but most UNIX SOs handle it fine. – anastaciu Apr 16 '21 at 19:32
  • 1
    You have 29 million bits. Each bit is written to a file as a single byte, a "0" or a "1". And you don't understand why you get a file with 29 million bytes in it? – Sam Varshavchik Apr 16 '21 at 19:36
  • I expected the file to be of size 3.7 MB. See my last update on the question. – GoldenRetriever Apr 16 '21 at 19:37
  • Why did you expect that? That's 3.7 million ***bits***, not bytes. You are writing out each bit, in the bitmask, as a single byte. There are 8 bits in a byte. So, for every bit, in the bitmask, you can 7 more bits in the file. – Sam Varshavchik Apr 16 '21 at 19:37
  • @SamVarshavchik, yes I understand. But I want 1 byte to actually hold 8 bits. How do I achieve this? – GoldenRetriever Apr 16 '21 at 19:38
  • 2
    I think you want this answer: [https://stackoverflow.com/a/4666996/487892](https://stackoverflow.com/a/4666996/487892) – drescherjm Apr 16 '21 at 19:38
  • Then, use a loop to take each group of 8 bits from the bit set, and write it out as a single byte. Not very convenient, this is not what `std::bitset` is for, but perfectly doable. – Sam Varshavchik Apr 16 '21 at 19:39
  • Bitsets - almost as good as plain ol' shifts and or's. Except when you have to do real work with them :) – Michael Dorgan Apr 16 '21 at 19:40
  • Damn shame you can't just `out.write(reinterpret_cast(&a), sizeof (a));`. It's trivially capable, and whatever overhead `bitset` has is bound to be minimal. – user4581301 Apr 16 '21 at 19:41
  • I still want to be able to do bitwise operations in `O(1)` time. Is it still possible if I pack the bits into a vector? @drescherjm – GoldenRetriever Apr 16 '21 at 19:42
  • That link is using a bitset. It just creates a way to save the bitset in a binary file instead of ascii '1' and '0' where each bit is saved as a character – drescherjm Apr 16 '21 at 19:46
  • Poking around a bit at this, I think you could simply `out.write(reinterpret_cast(&a), sizeof (a));` There's a bit of padding on the end, but that's it. A couple bytes is damn near invisible against the 3 Meg you are using. Does this port? Probably not. – user4581301 Apr 16 '21 at 20:50

0 Answers0