Set default value of dynamic array
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;
}
Related videos on Youtube
![peace_within_reach](https://i.stack.imgur.com/qT3OD.png?s=256&g=1)
peace_within_reach
Updated on May 10, 2022Comments
-
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 over 13 yearsIt's
bool *boolArray
, notbool boolArray
(that would be just a single bool, so the assignment ofnew something
wouldn't even make sense).
-
-
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 over 13 yearsUsing
std::vector
for this has some problems; see my answer. Also, while usingstd::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 over 13 yearsUhm, 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 onstd::queue
in my answer, but everyone has different preferences... Cheers, -
Cheers and hth. - Alf over 13 yearsuh, 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, withboost::array<bool, size>
, wheresize
is a run time value, won't compile. Cheers & hth.,