开发者

Set default value of dynamic array

开发者 https://www.devze.com 2023-01-26 09:19 出处:网络
This code will create an array of 100 elements and set the value of each to false. bool b开发者_如何转开发oolArray[100] = false;

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

bool b开发者_如何转开发oolArray[100] = false;

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

void Foo(int size)
{
    bool boolArray = new bool[size];
    //Now what?
}


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


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.


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


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号