Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I depend upon a new bool being initialized to false?

In C++, can I depend upon a new bool being initialized to false in all cases?

bool *myBool = new bool();

assert(false == *myBool);  // Always the case in a proper C++ implementation?

(Updated code to reflect comment.)

like image 356
WilliamKF Avatar asked Jun 28 '12 14:06

WilliamKF


People also ask

Is bool automatically false?

The default value of the bool type is false .

What are Booleans automatically initialized to?

The default value of Boolean is False .

What is bool initialized to in C++?

To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”. Instead, they are stored as integers: true becomes the integer 1, and false becomes the integer 0.

What is the default value for bool in C ++?

The default value of boolean data type in Java is false, whereas in C++, it has no default value and contains garbage value (only in case of global variables, it will have default value as false).


3 Answers

In this case, yes; but the reason is quite subtle.

The parentheses in new bool() cause value-initialisation, which initialises it as false. Without them, new bool will instead do default-initialisation, which leaves it with an unspecified value.

Personally, I'd rather see new bool(false) if possible, to make it clear that it should be initialised.

(That's assuming that there is a good reason for using new at all; and even if there is, it should be managed by a smart pointer - but that's beyond the scope of this question).

NOTE: this answers the question as it was when I read it; it had been edited to change its meaning after the other answer was written.

like image 147
Mike Seymour Avatar answered Oct 16 '22 18:10

Mike Seymour


The three relevant kinds of initialization, zero-initialization, default-initialization, and value-initialization for bool mean, respectively, that the bool is initialized to false, that the bool has an indeterminate value, and that the bool is initialized to false.

So you simply need to ensure that you're getting zero or value initialization. If an object with automatic or dynamic storage duration is initialized without an initializer specified then you get default-initialization. To get value-initialization you need an empty initializer, either () or {}.

bool b{}; // b is value-initialized
bool *b2 = new bool{}; // *b2 is value-initialized

class foo {
    bool b;
    foo() : b() {}
};
foo f; // // f.b is value-initialized

You get zero initialization for a bool that has static or thread local storage duration and does not have an initializer.

static bool b; // b is zero-initialized
thread_local bool b2; // b2 is zero-initialized

One other case where you get zero-initialization is if the bool is a member of a class without a user-provided constructor and the implicit default constructor is trivial, and the class instance is zero- or value-initialized.

class foo {
    bool b;
};
foo f{}; // f.b is zero-initialized
thread_local foo f2; // f2.b is zero-initialized
like image 5
bames53 Avatar answered Oct 16 '22 19:10

bames53


No. There is no automatic initialization in C++. Your new bool will be "initialized" to whatever was in memory at that moment, which is more likely to be true (since any non-zero value is true), but there is no guarantee either way.

You might get lucky and use a compiler that plays nice with you and will always assign a false value to a new bool, but that would be compiler dependent and not based on any language standard.

You should always initialize your variables.

like image 2
Fred Avatar answered Oct 16 '22 19:10

Fred