|
-
July 26th, 2005, 04:46 PM
#1
Array of Bools
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;
-
July 26th, 2005, 05:28 PM
#2
Re: Array of Bools
In VC7.0 and above, you can do the following:
Code:
data_seg(...);
static bool[255] KeyStatus = { 0 };
data_seg;
I don't believe the 'static' keyword is necessary, but I'm not sure.
Arjay
-
July 26th, 2005, 06:33 PM
#3
Re: Array of Bools
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?
-
July 26th, 2005, 07:11 PM
#4
Re: Array of Bools
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.
-
July 26th, 2005, 08:02 PM
#5
Re: Array of Bools
 Originally Posted by TiMBuS
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.
Regards,
Paul McKenzie
-
September 9th, 2005, 03:58 AM
#6
Re: Array of Bools
I have a question about
Code:
bool KeyStatus[255] = {0};
Why 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'.
How to solve this and create a an array of booleans that has an unknown size at compile time?
greets
-
September 9th, 2005, 04:48 AM
#7
Re: Array of Bools
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;
-
September 9th, 2005, 04:49 AM
#8
Re: Array of Bools
In C++, the syntax for declaration of arrays is as
decl-specifiers dname [ constant-expressionopt ] ;
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.
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. Like
Code:
std::vector< bool > DynamicBoolArray;
if you don't wanna go into the hassle of new/delete
-
September 9th, 2005, 06:01 AM
#9
Re: Array of Bools
I don't think you can assign the value 0 to all elements at once but just do a simple loop:
What about this?
Code:
bool* KeyStatus = new bool[myList.size()];
memset(KeyStatus, false, myList.size() * sizeof(bool));
-
September 9th, 2005, 07:17 AM
#10
Re: Array of Bools
 Originally Posted by cilu
What about this?
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.
-
September 9th, 2005, 07:37 AM
#11
Re: Array of Bools
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.
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.
-
September 9th, 2005, 07:46 AM
#12
Re: Array of Bools
 Originally Posted by timv
How to solve this and create a an array [..] that has an unknown size at compile time?
Using std::vector is the way to go -
Code:
// Construct a Dynamic Array comprising of "nNumBooleansInArray" elements
// Each member is given a default value "false"
std::vector <bool> vecDynamicBoolArray (nNumBooleansInArray, false);
A usage sample is -
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;
}
Note that when using std::vector one does not have to worry about Memory Allocation, De-allocation, leaks, copies, etc.
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 -
Last edited by Siddhartha; September 9th, 2005 at 12:12 PM.
Reason: One link less...
-
September 9th, 2005, 11:46 AM
#13
Re: Array of Bools
[ moved ]
Regards,
Siddhartha
-
September 9th, 2005, 11:55 AM
#14
Re: Array of Bools
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 9th, 2005, 12:08 PM
#15
Re: Array of Bools
 Originally Posted by Graham
No, no, no, no! Don't use vector<bool>.
Try std::bitset as a better alternative.
I appreciate this spectacular objection.
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).
Last edited by Siddhartha; November 7th, 2005 at 05:19 PM.
Reason: Softer... !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|