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).