tags:

views:

147

answers:

5

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.

A: 

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

hype
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.
meagar
Actually all elements do get initialized. The standard states that all remaining elements get set to 0.
Ben Voigt
@meagar how can you say that "int i[10] = {0}" does anything else than set 0 on the first element.
yan bellavance
@yan I'm pretty sure he can say that because that's how the language is defined to work.
Tyler McHenry
@Tyler McHenry sounds kind of odd to say that the only time you can initialize all elements of an array this way is when you intialize them to the value that is already used by default, which has a null effect, but if you say thats how the language is defined, I believe you.
yan bellavance
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.
Rob Kennedy
@Rob Kennedy I see.
yan bellavance
+4  A: 

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.

James Curran
When was it that the second version is initialized? I thought only if you explicitly default-initialize the containing class/struct?
jdv
@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?](http://stackoverflow.com/questions/3309042/what-does-main-return).
Johannes Schaub - litb
+2  A: 

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] << ' ';
meagar
A: 

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.

f0b0s
Actually all elements do get initialized. The standard states that all remaining elements get set to 0.
Ben Voigt
well i meant that. ok, my mistake.
f0b0s
`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.
Omnifarious
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.
f0b0s
+4  A: 

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);
Rob Kennedy