bool a[10];
a[0] = 1;
for (bool& e : a) cout << e;
Outputs 1000002000 instead of 1000000000, which I'd expect. Why is this?
If I make the array bigger the values get even more strange.
bool a[10];
a[0] = 1;
for (bool& e : a) cout << e;
Outputs 1000002000 instead of 1000000000, which I'd expect. Why is this?
If I make the array bigger the values get even more strange.
Your bool array is not initialized so it's full of garbage. If you print the array before changing the first index ([0]), you will see it's not your assignment which changes the other parts.
For initializing I'd suggest this line
bool a[10] = {false};
Did you notice that one of your bool values was 2?
That's one of many things that can happen when the behaviour of your program is undefined, which it is in your case since you are reading uninitialised non-char data.
bool a[10] = {};
is the fix.
Because the language is allowed to assume that a bool type can only store false or true, the << overload on ostream is permitted to use code like
e + '0'
when outputting a bool type e. That of course wreaks havoc with incorrectly initialised data!
You don't initialize the array.
Use memset, fill_n or for loop
memset(a, INIT_VALUE, sizeof(a)); // use only 0, -1 value, please.
fill_n(a, SIZE, INIT_VALUE); // or use this for save values.
for(bool &e : a) e = 0; // use loop init directly.