CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Array of Bools

  1. #1
    Join Date
    Jun 2005
    Posts
    241

    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;

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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

  3. #3
    Join Date
    Jun 2005
    Posts
    241

    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?

  4. #4
    Join Date
    Jul 2005
    Posts
    21

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of Bools

    Quote 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

  6. #6
    Join Date
    Feb 2005
    Posts
    218

    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

  7. #7
    Join Date
    Sep 2005
    Posts
    1

    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;

  8. #8
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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));
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Aug 2005
    Posts
    47

    Re: Array of Bools

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

  11. #11
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  12. #12
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Array of Bools

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

  13. #13
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Array of Bools

    [ moved ]

    Regards,
    Siddhartha

  14. #14
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  15. #15
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Array of Bools

    Quote 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... !

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured