I have two loops that iterate over an array of about 20 bools one after the other and print the memory address of each one:
for (int i = 0; i < 20; ++i) {
printf("%p\n", &_boolArray[i]);
}
for (bool b : _boolArray) {
b = true;
printf("%p\n", &b);
}
I would expect the output to be exactly the same for both arrays. What I got was something a little different:
0x102eeefb0
0x102eeefb1
0x102eeefb2
0x102eeefb3
0x102eeefb4
0x102eeefb5
...
0x7fff5ce8b9bf
0x7fff5ce8b9bf
0x7fff5ce8b9bf
0x7fff5ce8b9bf
0x7fff5ce8b9bf
I know there are better ways to fill an array (std::fill for one), but I still want to know why this is happening.
You are making a copy of b in second for loop every time, should pass by reference