5 ms·
No. The code in question is invoking undefined behavour, making the question pointless.
by deletes 13y ago
No. The code in question is invoking undefined behavour, making the question pointless.
- chris_wot 13y agoThen the answer is "yes and no".
- Dylan16807 13y ago"yes and no" is a poor way of putting anything. I prefer a nice clear statement like: You can access a local variable from outside its scope if and only if your code is not actually valid C++.
- ctdonath 13y agoValid C++ may have undefined behavior.
- ygra 13y agoI think there is a difference between syntactically valid and semantically valid. UB always puts you in the semantically invalid category.
- IsTom 13y agoCompiler 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.
- ctdonath 13y agoActually 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/n1905.pdf 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.
- mikeash 13y agoIt is often instructive and useful to look at the behavior of real implementations, not just the idealized behavior given by the standard.
- bentcorner 13y agoIndeed - sometimes you run into something and think "that shouldn't have worked", and it's useful to understand why.
- IsTom 13y agoWhen compiled with -O3 using g++ it returns 0 8. Compiler will use undefined behavior to optimize.
- mikeash 13y agoThere's a fun example with clang where you can have two pointers where: x == y But: *x != *y It involves invoking undefined behavior by using a pointer after it's been freed and arranging for a new pointer at the exact same location. Clang cached the contents of the old pointer and uses it in the comparison.
- deleted 13y ago[deleted]
- mcguire 13y ago...and by extension, many buffer-overflow security exploits do not actually exist.