Hacker News new | past | comments | ask | show | jobs | submit login

Compiler has the freedom to use assumption that the code will never hit undefined behavior. To actually access the variable you have to hit undefined behavior.



Actually the compiler (well, the author thereof) must know that the code may very well hit undefined behavior, and address how such known situations should be handled.

From the C++ spec (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n190...):

1.3.13 undefined behavior [defns.undefined]

behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements. Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior. [ Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]

1.3.14 unspecified behavior [defns.unspecified] behavior, for a well-formed program construct and correct data, that depends on the implementation. The implementation is not required to document which behavior occurs.

The spec goes on to explicitly state, over 150 times, certain situations result in undefined behavior; my favorite being:

Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic variable, might cause it to behave as if it is neither true nor false.




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

Search: