Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In standard C++ you can default-initialize just about anything, including that array:</p> <pre><code>bool* boolArray = new bool[size](); // Zero-initialized </code></pre> <p>Complete program that also checks the result, and deallocates the array:</p> <pre><code>bool foo( int size ) { bool* boolArray = new bool[size](); // Zero-initialized // Check that it is indeed zero-initialized: for( int i = 0; i &lt; size; ++i ) { if( boolArray[i] ) { delete[] boolArray; return false; } } delete[] boolArray; return true; } #include &lt;iostream&gt; int main() { using namespace std; cout &lt;&lt; (foo( 42 )? "OK" : "Ungood compiler") &lt;&lt; endl; } </code></pre> <p>Whether your compiler will accept or even do the Right Thing is another matter.</p> <p>So, in practice, <em>if</em> you feel an irresistible urge to use a raw array, then perhaps better use <code>std::fill</code> or some such, or even a raw loop.</p> <p>But note the repeated <code>delete[]</code>-expressions. Such redundant code is very easy to get wrong: it's Evil&trade;. And there's much else that can go wrong with use of raw arrays, so as a novice, just Say No&trade; to raw arrays and raw pointers and such.</p> <p>Instead, use standard library containers, which manage allocation, initialization, copying and deallocation for you &ndash; correctly. There is a little problem with that, though, namely a premature optimization in <code>std::vector&lt;bool&gt;</code>, which otherwise would be the natural choice. Essentially <code>std::vector&lt;bool&gt;</code> uses just one bit per value, so that it can't hand out references to <code>bool</code> elements, but instead hands out proxy objects&hellip;</p> <p>So, for <code>bool</code> elements, use e.g. a <code>std::bitset</code> (when the size is known at compile time), or e.g. a <code>std::deque</code>, as follows:</p> <pre><code>#include &lt;deque&gt; bool foo( int size ) { std::deque&lt;bool&gt; boolArray( size ); // Zero-initialized for( int i = 0; i &lt; size; ++i ) { if( boolArray[i] ) { return false; } } return true; } #include &lt;iostream&gt; int main() { using namespace std; cout &lt;&lt; (foo( 42 )? "OK" : "Ungood compiler") &lt;&lt; endl; } </code></pre> <p>Cheers &amp; hth.,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload