3D Array of Bool

This is a discussion on 3D Array of Bool within the C++ Programming forums, part of the General Programming Boards category; I program with the g++ compiler on a linux workstation. I am testing a very simple program that has only ...

3D Array of Bool

I program with the g++ compiler on a linux workstation. I am testing a very simple program that has only a 1000x1000x1000 array of boolean variables. I thought each boolean variable occupied only one bit. So the program would require appromixately 10^9 bits ~= 119 megabytes of RAM. However, the system monitor tells me that the program requires about 976 megabytes of RAM.

O..that explains why. But why would it use 1 byte if 1 bit can already represent 0 or 1. Anyway, is there any data type that uses only 1 bit? For what I am doing, even a 1000x1000x1000 array would not be enough.

>> But why would it use 1 byte if 1 bit can already represent 0 or 1.
Because bool doesn't just hold true or false, it also hold integers.

>> For what I am doing, even a 1000x1000x1000 array would not be enough.
If you're worried about room, make a matrix of chars instead, and alter each bit (0x80 for starters) to hold your "bool" value; so that way one char holds 8 "varibles".

O..that explains why. But why would it use 1 byte if 1 bit can already represent 0 or 1. Anyway, is there any data type that uses only 1 bit? For what I am doing, even a 1000x1000x1000 array would not be enough.

Thanks.

The way memory is structured it would probably be wasteful and time consuming traversing the whole of memory by bits.

[edit]
>>Would you please elaborate more? or give me a reference? It seems to be a good solution for my problem.

a char can hold 8bits worth of data. you can use each bit as a seperate variable, for example:
if you want to store the values true in the first bit of a char...

Code:

char a;
a |= true*2^0; //the zero is like the array index (if you were using an array)
a |= true*2^1; //stores true in the second bit
// and so on...
//i think this is how a bit goes from true to false
if (a & 2^0) //checks if the first bit is true
a ^= true*2^0; //if so, xor it with true to change it to false

the 2^x in bold means 2 to the power of x, not xor. i think having it written this way makes the (pseudo)code slightly clearer

[edit]
>>Would you please elaborate more? or give me a reference? It seems to be a good solution for my problem.

a char can hold 8bits worth of data. you can use each bit as a seperate variable, for example:
if you want to store the values true in the first bit of a char...

Code:

char a;
a |= true*2^0; //the zero is like the array index (if you were using an array)
a |= true*2^1; //stores true in the second bit
// and so on...
//i think this is how a bit goes from true to false
if (a & 2^0) //checks if the first bit is true
a ^= true*2^0; //if so, xor it with true to change it to false

the 2^x in bold means 2 to the power of x, not xor. i think having it written this way makes the (pseudo)code slightly clearer

Thanks, the syntax is probably too cumbersome for my problem. You once mentioned bitsets and I played with it. However, both bitset<1> and bitset<8> use 4 bytes after I checked with the function sizeof( ). Why?

> both bitset<1> and bitset<8> use 4 bytes after I checked with the function sizeof( ). Why?
They're still going to use the smallest amount of allocatable storage, no matter how few bits you choose.
Try something like say 1000 or 2000, then you should see the benefit if it really is using just bits internally.

I'm surprised that no one mentioned vector<bool>, which unlike bitset<> can be used if the size is determined at runtime. It has issues, but is worth knowing about, since even if it's removed from a future version of the standard, the functionality will probably be duplicated somehow.

Yes I know that the explicit specialisation is a hack that doesn't work exactly the same as every other data type, but for most purposes it works fine.
I'm sure boost has something better though.

I presume this is for voxel data? Have you considered compressing it? (beyond using 1-bit per voxel of course)
I mean you could probably use something like RLE to compress it in such a way that you can still quickly determine the status of an individual voxel. Reducing the memory usage by even say a factor of four would probably still provide a speed increase after decompression, as memory would probably be your bottleneck. Of course it depends on how often you write to it. If it is read in and then not changed, doing this should be sweet as!

Yes I know that the explicit specialisation is a hack that doesn't work exactly the same as every other data type, but for most purposes it works fine.
I'm sure boost has something better though.

I presume this is for voxel data? Have you considered compressing it? (beyond using 1-bit per voxel of course)
I mean you could probably use something like RLE to compress it in such a way that you can still quickly determine the status of an individual voxel. Reducing the memory usage by even say a factor of four would probably still provide a speed increase after decompression, as memory would probably be your bottleneck. Of course it depends on how often you write to it. If it is read in and then not changed, doing this should be sweet as!

You are right that it is for voxel data (I had to look up what voxel means). I am writing a Monte Carlo program that describes brownian motions.
I will definitely look into RLE, if it could reduce memory usage and still provide a speed increase. However, I always thought a compression would usually slow things down? not true? Also, I need to constantly change the true/false values in the array, since the particles move.