Dynamic array of type bool??

This is a discussion on Dynamic array of type bool?? within the C++ Programming forums, part of the General Programming Boards category; Hi all How to create a dynamic array of type bool and set all values to false?....I know this qs ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    22

    Dynamic array of type bool??

    Hi all
    How to create a dynamic array of type bool and set all values to false?....I know this qs is simple
    but I tried declaring a boolean array like
    Code:
    bool *sample;
    //later....
    sample = new bool [size];
    it gives me a bad malloc error.. please help!
    thanks
    regards
    KR

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    A bad malloc error? Anyway, you could write:
    Code:
    bool* sample = new bool[size]();
    You could also use a std::vector<bool>, except that it is a little different from normal vectors. What exactly do you plan to use this array of bool for?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    22
    The vector bool will contain true or false values if the some conditions are met.
    So each position in the bool vector will correspond to an object. If this object is tested and it passes.. the corresponding position in the bool vector changes to true.. else they remain false!
    Im i being clear here?
    thanks
    KR

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Right. You might want to consider using a boost::dynamic_bitset (it is a header-only library).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    22
    Thanks.. mean while... in the object constructor i just declared an empty bool array whose size i initialized later....in another function.. it still works.. can u tell teh difference.. if its right or not?

    Code:
    // in constructor
    bool bvec[]
    
    // in a function called a()
    bvec[size];
    is this ok?.. how different is it from dynamic vector?
    thanks
    regards

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    What exactly is your class definition?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    22
    Code:
    class b
    {
    bool * bvalue; // error is here!
    void bsizesetter(int size);
    .....
    // a lot of functions and variables;
    }
    
    b::b()
    {
    bvalue = 0;
    }
    
    b::bsizesetter(int size)
    {
    bvalue = new bool[size];
    }

    Thhis is waht i try to do.
    some things i tried are
    a. A bool pointer is not declared even if i try
    b. static bool array even if declared cannot be reinitiated using "size" variable. I dont know the value of size here untill a certain operation is done. SO i cant declare the bool array with a fixed size in contructor.
    so please suggest what i can do.....
    thanks
    regards
    -
    KR

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    There cannot possibly be an error on that line since it is syntactically correct. Post the smallest and simplest (compilable) program that demonstrates the error, and then state what the error is, in full. Don't bother telling us in detail whatever else you tried; tell us your best attempt in full, and exactly what you want to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    22
    ok sorry for that....
    in class defnition
    Code:
    class object
    {
    vector<bool> B;
    void someoperation();
    }
    
    void object::someoperation()
    {
    // operations done.....
    int size = somevector.size(); // this step determines size of the bool vector
    
    B(size,false);
    
    }
    All i try to do is create a vector variable of type bool to store a string of false values... I dont know the size of the vector. henc ei declare it in this function
    The code is too large to post here. I have esxplained to you to my max level of the problem.
    How would you declare a vector<bool> variable in a class. and assign its values later in another function of the same class?
    thats my doubt to be perfect....
    thanks
    regards
    KR

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    22
    Hey the problem was solved....
    Its simple to degine a dyn array of bool
    How i tried was define a global
    typedef bool* typedefvariable;
    and used it to declare bool ptr variables inside the class.
    thanks for the help.. laserlight!
    regards
    KR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21