Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think UB doesn't have much to do with this bug after all.

The original code defined a struct with two bools that were not initialized. Therefore, when you instantiate one, the initial values of the two bools could be anything. In particular, they could be both true.

This is a bit like defining a local int and getting surprised that its initial value is not always zero. (Even if the compiler did nothing funny with UB, its initial value could be anything.)





The entire "its initial value could be anything" is one of the possible consequences of UB. It is not the most dire but in C and C++ it is an outcome from an UB.

Could a language define un-initialized variables as readable garbage? Sure, but that would be a different language with different semantics, and such languages can also define declaration such that

> defining a local int and getting surprised that its initial value is not always zero.

is in fact reasonable. That is what Java and Go opted to do, for instance.


> The original code defined a struct with two bools that were not initialized. Therefore, when you instantiate one, the initial values of the two bools could be anything. In particular, they could be both true.

Then reading from that struct like in OP constitutes UB.


Well yes, that would be UB, but even if the C++ compiler had no concept of UB, it would still be wrong code.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: