Allocation of Booleans?

How much memory is allocated for a single boolean value? Just a bit? Do they allocate a whole byte to keep a consistent pattern? Or does it vary across compilers?
A whole byte. It's not possible to allocate a single bit in any modern computer (that I know of, anyway).
However, some structures, such as std::vector, have specializations that make each bool element use only one bit.
So then, how would I go about using the vector class to store bits?
The same way you would use a vector to store anything else.

Remember, though, that the computer cannot use less memory for any one object than a byte. You can pack multiple bit-booleans into a single byte... but that's all.

Specializing the vector class on bits was a mistake in the STL, alas.



What exactly are you trying to do?
Feel free to call me a fool, but hey, it's worth a shot.

A program I use called 'GameMaker' allows its variables to represent any value. Whether it be an int, a string, a char, or a pointer, you can use it. I know that you can use a void pointer for this, but I thought that this would be a nice little project. Even if it fails, it would be a nice learning experience.

Basically, my idea is to make a class where the objects could represent any value without the need for type casting. I was thinking of using malloc to allocate the necessary memory, but apparently the vector class should handle it for me. The only problem I could see with this would be handling pointers, since there isn't really any way to get around without the user having to use type casting in some way.

Although, after writing this, I feel like this would be pretty much a worthless project. I couldn't see much of a practical use for this even if it is successfully made.
Last edited on
You might want to have a look at VB's VARIANT. It uses a tag and a union to represent a variable of "any type".
I might be misunderstanding, but this sounds like the antithesis of C++ programming. C++ wants to be strongly typed; what you are doing is trying to completely eliminate C++'s typing system.

How exactly does a user use your type? What are it's API methods? What can a user do with this type?

Depending on your answer, perhaps boost::any would fit the bill.

Pretty much what jsmith said, while also adding that implementing this as a separate language would probably be more adequate.
My take on the matter is:

Just because GameMaker's implemented in C++, it doesn't follow that GameMaker's variables should be strongly typed. It all depends on how they're used. If all you do is hold numbers and strings, if doesn't really matter how they're represented.

Keywords can be used to imply type when necessary, that's a fairly common convention.
The variables in GM can hold any values. An object, a number, text, a pointer, anything. But since a lot of things are done without the user ever even seeing it, I'm not entirely sure how it works.

I might be misunderstanding, but this sounds like the antithesis of C++ programming. C++ wants to be strongly typed; what you are doing is trying to completely eliminate C++'s typing system.


This library is meant to be used to allow people to move from GML (GM's language) to C++ easily and still be able to make games quickly. However, I've realized that if people can't even figure out how to declare variables, they probably won't get too far anyways.
I prefer strongly typed languages. If I want a user to pass a string, I don't want to have to do the checking myself and warn the user, I want them to get an error for being stupid.
That's another thing. Better to let the user know that they're using the wrong variable than to let them get away with it and have them think there's something wrong with the library.
Well with boost::any you can do things like:

1
2
3
4
5
boost::any a( 4 );    // store int
a = std::string( "hello world" );   // store std::string
a = "Hello world";  // const char*
a = 3.14;   // double
a = std::vector<int>( 3, 0 );  // vector<int>  


The trick is that if you ever want to get the value out of the boost::any, you have to know its type:

1
2
3
4
5
6
7
8
9
10
11
// Version returning (const) reference:
try {
    const std::string& str = boost::any_cast<const std::string&>( a );
} catch( const boost::bad_any_cast& ) {
    std:: cout << "Contained type is not std::string" << std::endl;
}

// Version returning (const) pointer:
const std::string* str = boost::any_cast<const std::string*>( a );
if( !str )
    std::cout << "Contained type is not std::string" << std::endl;


Topic archived. No new replies allowed.