6

This code will create an array of 100 elements and set the value of each to false.

bool boolArray[100] = false;

How can I set the default value of a dynamic array?

void Foo(int size)
{
    bool boolArray = new bool[size];
    //Now what?
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
peace_within_reach
  • 237
  • 2
  • 4
  • 14

4 Answers4

12

Use std::fill function or std::fill_n function.

std::fill_n(boolArray, length, defaultValue);
std::fill(boolArray, boolArray + length, defaultValue);

Side note: try using std::vector instead.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Using `std::vector` for this has some problems; see my answer. Also, while using `std::fill` is certainly a good solution to a generalization of the OP's problem, it's not necessary for the particular problem at hand. Again, see my answer. Cheers & hth., – Cheers and hth. - Alf Nov 24 '10 at 02:35
12

In standard C++ you can default-initialize just about anything, including that array:

bool* boolArray = new bool[size]();     // Zero-initialized

Complete program that also checks the result, and deallocates the array:

bool foo( int size )
{
    bool* boolArray = new bool[size]();     // Zero-initialized

    // Check that it is indeed zero-initialized:   
    for( int i = 0; i < size; ++i )
    {
        if( boolArray[i] ) { delete[] boolArray; return false; }
    }
    delete[] boolArray; return true;
}

#include <iostream>
int main()
{
    using namespace std;
    cout << (foo( 42 )? "OK" : "Ungood compiler") << endl;
}

Whether your compiler will accept or even do the Right Thing is another matter.

So, in practice, if you feel an irresistible urge to use a raw array, then perhaps better use std::fill or some such, or even a raw loop.

But note the repeated delete[]-expressions. Such redundant code is very easy to get wrong: it's Evil™. And there's much else that can go wrong with use of raw arrays, so as a novice, just Say No™ to raw arrays and raw pointers and such.

Instead, use standard library containers, which manage allocation, initialization, copying and deallocation for you – correctly. There is a little problem with that, though, namely a premature optimization in std::vector<bool>, which otherwise would be the natural choice. Essentially std::vector<bool> uses just one bit per value, so that it can't hand out references to bool elements, but instead hands out proxy objects…

So, for bool elements, use e.g. a std::bitset (when the size is known at compile time), or e.g. a std::deque, as follows:

#include <deque>

bool foo( int size )
{
    std::deque<bool> boolArray( size );     // Zero-initialized

    for( int i = 0; i < size; ++i )
    {
        if( boolArray[i] ) { return false; }
    }
    return true;
}

#include <iostream>
int main()
{
    using namespace std;
    cout << (foo( 42 )? "OK" : "Ungood compiler") << endl;
}

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2
bool* boolArray = new bool[size];
for(int i = 0; i < size; i++) {
    boolArray[i] = false;
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • +1: not as "elegant" as Mehrdad's std::fill suggestion (also +1-ed), but so fundamental and re-purposeable it's a great, productive approach to reach for when unsure. – Tony Delroy Nov 24 '10 at 02:21
0

What about:

void Foo(int size)
{
    // bool boolArray = new bool[size];
    // Did you mean bool*?
    // Try and avoid direct allocation of memory.
    // Memory allocation should be done inside an object that 
    // actively manages it.

    // Normally I would recommend a vector
    std::vector<bool>   boolArray(size, false);

    // But. And a Big but. Is that the standards committee decided to
    // specialize the vector for bool so that each element only takes
    // a single bit. Unfortunately this had some side effects that were
    // made its use not perfect (time/assign-ability).

    // So we can try a boost array
    boost::array<bool, size>   boolArray;
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Uhm, just a nit, you can't use run-time `size` as template argument (as in the code as it is as I'm writing this). Perhaps boost has array that allows run-time size? I landed on `std::queue` in my answer, but everyone has different preferences... Cheers, – Cheers and hth. - Alf Nov 24 '10 at 03:42
  • uh, typo, i meant i landed on `std::deque` in my answer, as can be seen in the answer... but anyway, your code as it is right now, with `boost::array`, where `size` is a run time value, won't compile. Cheers & hth., – Cheers and hth. - Alf Nov 24 '10 at 03:53