Undefined behavior is a harsh mistress
Pascal Cuoq - 28th Jun 2012Mark Shroyer has discovered an interesting consequence of undefined behavior in the compiled version of a C program: a variable behaves as both true and false as a result of being uninitialized.
The post is great and could not come at a better time. I needed for the talk I will give on Saturday morning at COMPARE2012 convincing examples that some undefined behaviors cannot be recovered from. For a long time I had wanted an example that “uninitialized” was not equivalent to “holding an unknown value” and this is just it. Once again procrastination wins!
One a side note should I be worried that I am building tolerance to the panic of not having one's slides ready for an upcoming talk? If I can't even get in a frenzy in the last few days before the talk I may just become useless. Perhaps it's time for retirement.
The only disappointing aspect of Mark's blog post is the general reaction to it in the blog's comments or elsewhere. Guys Mark obviously knows what undefined behavior is! He is not blaming GCC for not giving a consistent value to his variable p
. Using uninitialized memory is a bug in his program; it's a bug he has been looking for and the consequences of it were funny so he shared. No need to get all professorial about it.
The so-stupid-it's-funny prize goes to the comment below.
First you demonstrate ignorance talking about C language with the “bool” data type.
Dude Mark demonstrates his mastery of C by properly using stdbool.h
as part of a sound discussion of undefined behavior in a C program.
If you are in the Manchester area and are reading this please drop by on Saturday. Gate-crash the conference if you have to. You can always blame my bad influence if you get caught.