The following is an abbreviated definition structshowing only the problem.
struct Entry {
std::array<std::array<bool, DIM>, DIM> filled;
std::array<std::array<char, MAX>, MAX> cells;
std::vector<Constraint> overlaps;
std::vector<Hole*>::iterator candidates;
Entry() = default;
};
It was a mistake. I thought the default constructor would be zero to initialize arrays, but it just fills them with random garbage. Now I know that I need to write a default constructor, but I am confused by the behavior that I encountered when testing.
This is a shortened version of my test function:
void testEntry(void) {
Entry e;
std::cout << std::boolalpha;
e.cells[1][2] = 'a';
e.filled[0][0] = true;
for (int i = 0; i < MAX; ++i)
for (int j = 0; j < MAX; ++j)
std::cout<<i<<" "<<j<<" "<<e.cells[i][j]<<std::endl;
for (int i = 0; i < DIM; ++i)
for (int j = 0; j < DIM; ++j)
std::cout<<i<<" "<<j<<" "<<e.filled[i][j]<<std::endl;
}
When I ran it, arrays filledand cellscontain random garbage, and eventually I found my mistake. During debugging, I changed the declaration Entry e;to Entry e{};, and the changed code works as I expected, but I don’t understand why.
. , : " braced-init , T - , ." , , , .
, zero-intialization
" T - , , . , , ", .
, , . ( clang Xcode 7.2.1).