Archived

This topic is now archived and is closed to further replies.

Kingstone426

Wasting RAM with < 8 bit datatypes.

Recommended Posts

Perhaps this is a really stupid question but it has been irritating me for some time. If I declare a datatype that uses less than 1 byte of RAM still at least one byte will be reserved as you cannot adress specific bits in the memory directly. Thus: byte myByte; and bool myBool; uses the same amount of RAM. 7 bits of dataloss for the bool, perhaps not so terribly much in this example but if I have say 10 million bools in an array I will lose like 9 MB right? I''ve never read about anyone experiencing this problem so maybe my 10 million bool example is just stupid. Also it could pretty easily be solved by declaring it as a byte array and using bit operations. But lets say we have an OO project with 10 million objects where each one has a bool... Perhaps even more stupid but anyway I''m just wondering if this is a real problem or if somehow the compiler manages it or whatever. Is it different in different languages?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
bools are supposed to be 8-bit.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Partial register access kills you, which is why no one makes a bool just one bit. Like you said, you can use bitmasks and even bitvectors to get around this.

Share this post


Link to post
Share on other sites
The reason is simply that you cannot access memory bit-wise directly. Bit-wise access would slow down bool operations at critical points to unacceptable values.

You can use std::vector that has a specialization for bool, which uses every bit of memory

you can also define the number of bits a variable uses in structs and classes, e.g.

struct test
{
bool a : 1;
int b : 2;
int c : 2;
};

[edited by - novum on May 6, 2003 5:14:28 PM]

Share this post


Link to post
Share on other sites
Not that these structures need to be 8 bit aligned. So the shown example won''t compile.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
I checked that after reading the post.

quote:

Microsoft Specific

In Visual C++ 4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers.


Share this post


Link to post
Share on other sites
yeah its kind of a sucky problem, but dont expect to be able to declare 1 bit of memory one day: the basic block of 8 bits=1 byte is rooted quite toroughly in cpu design..

bitmasks are indeed your friend here though. if you can combine multiple flags in one byte, and read them out using a bitmask, you lose almost no speed at all, and memory waste isnt that awfull anymore.

Share this post


Link to post
Share on other sites