11

I am trying to initialize an array of bools like so:

bool FcpNumberIsOk[MAX_FCPS]={true};

but when I debug it, I only see the first element of the array initialized, the others are false. How can that be so? I am using Qt on ubuntu 10 and the initialization is done on a local array inside a method.

Ok thanks for your answers.

5 Answers 5

13

You've misunderstood. It appears that you though that any unmentioned elements would get initialized to the same value as the last explicitly initialized value. The last value you mentioned was true, so all further elements would be initialized to true as well. I once had that same belief, but I quickly learned otherwise.

That's not how it works. Any unmentioned elements get default-initialized, which for bool means false.

To set all the elements to true, try something like std::fill_n:

std::fill_n(FcpNumberIsOk, MAX_FCPS, true);
Sign up to request clarification or add additional context in comments.

Comments

9

Because that's the way array initialization works in C++. If you don't explicitly give a value for each element, that element defaults to zero (or, here, false)

 bool FcpNumberIsOk[MAX_FCPS]={true, true, true, true /* etc */ };

Note that

 bool FcpNumberIsOk[MAX_FCPS];

Will set all values to false or have them set randomly, depending on where this is defined.

4 Comments

When was it that the second version is initialized? I thought only if you explicitly default-initialize the containing class/struct?
@jdv, if you define the variable as having static storage duration (local/class statics, namespace scope variables), it is zero initialized at program start before anything else takes place. See What does main return?.
@JohannesSchaub-litb The link just rephrases what you said.
@ross no, I rephrased what the link says.
2

This is expected behaviour. The first element is initialized to the specified value and the remainder are initialized to the default value of 0:

int c[5] = {1};

// 1 0 0 0 0
for(int i = 0; i < 5; ++i)
  std::cout << c[i] << ' ';

Comments

-1

Because you have explicitly initialized only the first element of the array, only the first element is initialized and the remaining are not.

6 Comments

The remainder are initialized to a default of 0. This is why it is safe to use int i[10] = {0}; to 0-initialize an array.
Actually all elements do get initialized. The standard states that all remaining elements get set to 0.
@meagar how can you say that "int i[10] = {0}" does anything else than set 0 on the first element.
@yan I'm pretty sure he can say that because that's how the language is defined to work.
But @Yan, it's not already used by default. If you hadn't given any initializer at all, then the array would (usually) be uninitialized. The array could contain a mix of true and false, as well as values that aren't valid bool values at all. If you initialize any of the elements, then C++ provides a shorthand for giving default values to the remaining elements. That's not a null effect. I think there's merit in having the language work the way you (and I, once) thought it did, but it's too late to change it now. Code out there already relies on the current behavior.
|
-1

Using this syntax you are only initializing the first element (with your's value and other get default-one [false]), but not others. You should use either int array and memset or for loop to initialize all elements.

4 Comments

Actually all elements do get initialized. The standard states that all remaining elements get set to 0.
well i meant that. ok, my mistake.
memset will work, most of the time, but isn't portable. ::std::fill is much better and will work on a bool array, and on a good compiler it will be just as efficient.
i had a bad experience with std::copy with vectors on msvc. so, for the efficiency i use memcpy and memset functions. the are 100% fast. But yeah, I'm always really careful with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.