Thread: Dynamic array of type bool??

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Right. You might want to consider using a boost::dynamic_bitset (it is a header-only library).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    What exactly is your class definition?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07:05 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM