Wednesday, May 22, 2013

How much memory is taken by boolean array?

I run into a problem which is solved by BitSet. But I asked myself, why not just using an array of boolean?


Basically, Java takes a byte, instead of a bit, to store a bool. So it ends up using an array of bytes.

Same for C++.

And think it over, malloc allocates memory in bytes, isn't it?

void* malloc (size_t size);
Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

So no wonder this happens.

Use Bitset instead.

No comments:

Post a Comment