I would like to create an array of 255 Bools in a data_seg but I am not sure if im doing it right (the array and the data_seg part)?
Would I also have to enter 255 values?
data_seg(...);
static bool[255] KeyStatus = ???;
data_seg;
Printable View
I would like to create an array of 255 Bools in a data_seg but I am not sure if im doing it right (the array and the data_seg part)?
Would I also have to enter 255 values?
data_seg(...);
static bool[255] KeyStatus = ???;
data_seg;
In VC7.0 and above, you can do the following:
I don't believe the 'static' keyword is necessary, but I'm not sure.Code:data_seg(...);
static bool[255] KeyStatus = { 0 };
data_seg;
Arjay
I am still getting a few errors like ; expected or missing and stuff like that when i have:
static bool[255] KeyStatus = { 0 };
but not when I have
static bool KeyStatus = { 0 };
how can i fix this?
Um, from what I know, you declare arrays in C++ by specifying the number of members in the array in [] _after_ the name of the variable in a variable declaration.
so you should have
Code:bool KeyStatus[255] = {0}; //note that this only makes the first element of the array equal 0.
Your comment is wrong. All of the elements are set to 0 in the array.Quote:
Originally Posted by TiMBuS
Regards,
Paul McKenzie
I have a question aboutWhy is it again that the size of the array has to be constant? I want to have bool KeyStatus[myList.size()] = {0}; But the compiler complains 'expected constant expression'.Code:bool KeyStatus[255] = {0};
How to solve this and create a an array of booleans that has an unknown size at compile time?
greets
To create the array dynamically you have to do:
bool* KeyStatus = new bool[myList.size*()];
I don't think you can assign the value 0 to all elements at once but just do a simple loop:
for(int i=0; i<myList.size*(); i++)
KeyStatus[i] = 0;
Don't forget to delete the array when you're finished with it:
delete [] KeyStatus;
In C++, the syntax for declaration of arrays is as
The number of elements in the array is given by the constant-expression. The first element in the array is the 0th element, and the last element is the (n-1th) element, where n is the size of the array. The constant-expression must be of an integral type and must be greater than 0.Quote:
decl-specifiers dname [ constant-expressionopt ] ;
For further details, have a look at Array , you you can get this topic convered in any C++ book (in Arrays chapter).
For dynamic arrays, you can use vector of bool. Likeif you don't wanna go into the hassle of new/delete ;)Code:std::vector< bool > DynamicBoolArray;
What about this?Quote:
I don't think you can assign the value 0 to all elements at once but just do a simple loop:
Code:bool* KeyStatus = new bool[myList.size()];
memset(KeyStatus, false, myList.size() * sizeof(bool));
That's a bad way to go because it depends on a specific compiler implementing bool as 1 byte. It may fail on another compiler.Quote:
Originally Posted by cilu
The catch is that as long as false == 0, it doesn't matter what the sizeof(bool) is. Because two zeros on 1 byte or 165 bytes are equal. However, that doesn't work if want to set it to true, and true == 1. If bool had 2 bytes, each value would actually be 0x0101.Quote:
That's a bad way to go because it depends on a specific compiler implementing bool as 1 byte. It may fail on another compiler.
Using std::vector is the way to go -Quote:
Originally Posted by timv
A usage sample is -Code:// Construct a Dynamic Array comprising of "nNumBooleansInArray" elements
// Each member is given a default value "false"
std::vector <bool> vecDynamicBoolArray (nNumBooleansInArray, false);
Note that when using std::vector one does not have to worry about Memory Allocation, De-allocation, leaks, copies, etc.Code:#include <vector>
#include <iostream>
int main (void)
{
unsigned int nNumBooleansInArray = 0;
std::cout << "Please enter the number of boolean objects you wish to create: " << std::endl;
std::cin >> nNumBooleansInArray;
// Construct a Dynamic Array comprising of "nNumBooleansInArray" elements
// Each member is given a default value "false"
std::vector <bool> vecDynamicBoolArray (nNumBooleansInArray, false);
// This is just to show that you can use a vector as any array -
for (unsigned int nCount = 0; nCount < vecDynamicBoolArray.size (); ++nCount)
std::cout << "The boolean element at position " << nCount << " has value: " << vecDynamicBoolArray [nCount] << std::endl;
return 0;
}
Now, (at least on) MS VC++ 7.0, a specialization best suited to your requirement exists.
MSVC's version of STL comes with a specialization of vector called vector <bool> that specializes std::vector <T> for type bool.
Some references -
[ moved ]
Regards,
Siddhartha
No, no, no, no! Don't use vector<bool>. The standard specifies a specific implementation of this, and it's bad. Awful. Wrong.
Try std::bitset as a better alternative.
I appreciate this spectacular objection.Quote:
Originally Posted by Graham
However, you might contemplate reading the thread a bit... std::bitset is not an alternative to a dynamic array (which is what was asked for.)
std::bitset is suited to storing boolean flags, true - but the number of flags to be stored is decided at compile time.
Hence, suggesting std::bitset as an alternative to dynamic arrays as in this case is IMO - incorrect.
bitset is not a dynamic container. You cannot store more elements than specified at compile time. Period.
std::bitset is a container of choice only when one knows how many flags are to be stored, and only in that case one should one use it as the flags stored consume 1/8th the space - advantage of a bit over a bool (on MSVC).
To use a genuine std::vector <T> one can specify T as unsigned char.
The sizeof (unsigned char) and sizeof (bool) on MSVC (and other compilers) are same.
EDIT: It seems that std::bitset is a non-STL container (source).
Quote:
Originally Posted by MSDN
OK. OK. I missed the earlier references to bitset. So shoot me.
But still don't use vector<bool>...
No, we need you here... ;)Quote:
Originally Posted by Graham
a bool can be converted to an integer, and in that case, false is converted to zero, but i am not sure that false must be stored as zero. It may be stored as 42. But maybe the standard explicits something about the usage of memset on boolean arrays...Quote:
Originally Posted by cilu
false is always zero.Quote:
Originally Posted by SuperKoko
Hence, this -
is actually evaluated (since the legacy "C" days when no bool existed) as -Code:if (mSomeCondition)
{
// Do Something...
}
Here are some relevant quotes (source) -Code:if (mSomeCondition != 0)
{
// Do Something...
}
And...Quote:
4 An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.
Actually, across many programming languages, I have observed that -Quote:
If the source type is bool, the value false is converted to zero and the value true is converted to one.
For instance, in VB: vbTrue has a value of -1, whilst vbFalse is 0 (Zero).
- false is always zero.
- true can take any non-zero value.
Hence, when one writes a C++ COM Component that transacts with a VB client (or vice-versa) one transacts VARIANT_BOOL data type to convey boolean information, and VARIANT_FALSE is valued at 0 (Zero) whilst VARIANT_TRUE is -1 (and could be anything else for that matter).
Right, because "-1" is the signed form of 0xFFFFFFFF.
Viggy
Perhaps. But what compiler would store false as 42 if it must be converted to 0, and viceversa, from 0 to false? Frankly, this looks to me like a risk I would take... ;) (as the proverb says, he, who doesn't risk, does not win)Quote:
It may be stored as 42.
Yes, the risk is inexistant.Quote:
Originally Posted by cilu
Even, very weird compilers on very weird platforms probably use 0 to represent false.
But, on this point, i wonder why there is not a std::set template function that could assign all the items of an array to a specific value.
And this function could have specialized versions for primitive types (including bool) that could use memset.
And it could even be used to set all items of an array of boolean to true.
One can initialize the elements contained in a vector to a value via the constructor - as seen in my previous sample.Quote:
Originally Posted by SuperKoko
Otherwise std::transform comes close.
(The programmer needs to define the action to be taken.)
vector <bool> and std::bitset both feature:Quote:
Originally Posted by SuperKoko
...That can toggle boolean element states (which by default are constructed in a false state).