Set default value of dynamic array

18,065

Solution 1

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

Solution 2

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.

Solution 3

bool* boolArray = new bool[size];
for(int i = 0; i < size; i++) {
    boolArray[i] = false;
}
Share:
18,065

Related videos on Youtube

peace_within_reach
Author by

peace_within_reach

Updated on May 10, 2022

Comments

  • peace_within_reach
    peace_within_reach about 2 years

    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
      ThiefMaster over 13 years
      It's bool *boolArray, not bool boolArray (that would be just a single bool, so the assignment of new something wouldn't even make sense).
  • Tony Delroy
    Tony Delroy over 13 years
    +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.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 13 years
    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
    Cheers and hth. - Alf over 13 years
    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
    Cheers and hth. - Alf over 13 years
    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<bool, size>, where size is a run time value, won't compile. Cheers & hth.,