Quantcast
Channel: Does the C++ standard allow for an uninitialized bool to crash a program? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by Barmar for Does the C++ standard allow for an uninitialized bool to crash a program?

$
0
0

A bool is only allowed to hold the implementation-dependent values used internally for true and false, and the generated code can assume that it will only hold one of these two values.

Typically, the implementation will use the integer 0 for false and 1 for true, to simplify conversions between bool and int, and make if (boolvar) generate the same code as if (intvar). In that case, one can imagine that the code generated for the ternary in the assignment would use the value as the index into an array of pointers to the two strings, i.e. it might be converted to something like:

// the compile could make asm that "looks" like this, from your source
const static char *strings[] = {"false", "true"};
const char *whichString = strings[boolValue];

If boolValue is uninitialized, it could actually hold any integer value, which would then cause accessing outside the bounds of the strings array.


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>