Consider this program:
#include <stdio.h> int main(void) { unsigned int a; printf("%u %u\n", a^a, a-a); return 0; }
Is it undefined behaviour?
On the face of it, a
is an uninitialized variable. So that points to undefined behaviour. But a^a
and a-a
are equal to 0
for all values of a
, at least I think that is the case. Is it possible that there is some way to argue that the behaviour is well defined?
In computer programming, undefined behaviour is defined as 'the result of compiling computer code which is not prescribed by the specs of the programming language in which it is written'.
When we run a code, sometimes we see absurd results instead of expected output. So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.
In C the use of any automatic variable before it has been initialized yields undefined behavior, as does integer division by zero, signed integer overflow, indexing an array outside of its defined bounds (see buffer overflow), or null pointer dereferencing.
Is there a piece of JavaScript code for which the behaviour is not completely determined by the JavaScript specifications, and, as such, has "undefined behaviour"? Yes, read the quirksmode.
In C11:
a
never has its address taken (quoted below)Certain object representations need not represent a value of the object type.
Unsigned ints can have trap representations (e.g. if it has 15 precision bits and 1 parity bit, accessing a
could cause a parity fault).
6.2.4/6 says that the initial value is indeterminate and the definition of that under 3.19.2 is either an unspecified value or a trap representation.
Further: in C11 6.3.2.1/2, as pointed out by Pascal Cuoq:
If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.
This doesn't have the exception for character types, so this clause appears to supersede the preceding discussion; accessing x
is immediately undefined even if no trap representations exist. This clause was added to C11 to support Itanium CPUs which do actually have a trap state for registers.
Systems without trap representations: But what if we throw in &x;
so that that 6.3.2.1/2's objection no longer applies, and we are on a system that is known to have no trap representations? Then the value is an unspecified value. The definition of unspecified value in 3.19.3 is a bit vague, however it is clarified by DR 451, which concludes:
Under this resolution, int a; &a; int b = a - a;
results in b
having indeterminate value still.
Note that if the indeterminate value is not passed to a library function, we are still in the realm of unspecified behaviour (not undefined behaviour). The results may be weird, e.g. if ( j != j ) foo();
could call foo, but the demons must remain ensconced in the nasal cavity.
Yes, it is undefined behavior.
Firstly, any uninitialized variable can have "broken" (aka "trap") representation. Even a single attempt to access that representation triggers undefined behavior. Moreover, even objects of non-trapping types (like unsigned char
) can still acquire special platform-dependent states (like NaT - Not-A-Thing - on Itanium) that might appear as a manifestation of their "indeterminate value".
Secondly, an uninitialized variable is not guaranteed to have a stable value. Two sequential accesses to the same uninitialized variable can read completely different values, which is why, even if both accesses in a - a
are "successful" (not trapping), it is still not guaranteed that a - a
will evaluate to zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With