Check out this little GCC gem that I just discovered read about.

In GCC 1.17, when the compiler encountered specific forms of undefined behavior (unknown/not implemented #pragmas), here’s the code it executed:

execl("/usr/games/hack", "#pragma", 0); // try to run the game NetHack

execl("/usr/games/rogue", "#pragma", 0); // try to run the game Rogue

// try to run the Tower's of Hanoi simulation in Emacs. execl("/usr/new/emacs", "-f","hanoi","9","-kill",0);

execl("/usr/local/emacs","-f","hanoi","9","-kill",0); // same as above

fatal("You are in a maze of twisty compiler features, all different");[/c]

When GCC identified “bad” C++ code, it tried to start NetHack, Rogue, or Towers of Hanoi. Failing all three, it would just print out a nice, cryptic error message. Wow.

This just goes to show you that when the official C++ specification talks about undefined behavior, they mean business:

[Undefined behavior is] behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which the Standard imposes no requirements.

No requirements indeed.


It turns out this is actually implementation-defined behavior, which is slightly different than undefined behavior. Implementation-defined behavior actually requires the compiler to document what it does, but there are no requirements on what it can do.

(If you liked this, you might like Cheating in Video Games.)

Thanks for reading! RSS Feed Icon

Feross Aboukhadijeh I'm Feross, a programmer, entrepreneur, open sorcerer, and mad scientist.

I build WebTorrent, a torrent library for the web, WebTorrent Desktop, the best desktop torrent app, and Standard, a JavaScript linter. In my free time, I build Play, a music video app and NodeFoo, a Node.js documentation site.

I also maintain 100+ packages on npm. All my code is freely accessible on my GitHub page. If you like my work, support me on Patreon. Thanks to all my awesome supporters!

Lastly, I run Study Notes, a site to help students study better and get into college, and have done that since I was in high school myself.

If you enjoyed this article, you should follow me on Twitter or sign up to get an email whenever I write something new:

Share this article with your friends: