C and C++ standardese for "Don't do that."

This term is used by compiler standards documents to describe bad situations created by language constructs. Although these constructs are well-formed within the language specification, compiler vendors are not required to guard against them. Worse than unspecified behavior.

A favorite maxim of comp.lang.c++.moderated states that "undefined behavior" means anything, up to and including reformatting your hard drive, can result, but this is gross understatement. Undefined behavior means that anything, up to and including nuclear war, may result. This is not a joke.

Well-known undefined behavior situations include:
  • Deleting a pointer to a base type, which holds a pointer to a derived type, when the base type does not have a virtual destructor.

    class base { };

    class derived: public base {};

    // meanwhile, back at the ranch ...
    {
     base *p;

     p = new derived;

     delete p;    // BOOM!
    }

  • Accessing and modifying an lvalue without an intervening sequence point.

    char *p = "Stuff";
    int i = 82;
    p [i] = ++i;    // BOOM!
Now, of course, it would be nice if a compiler could flag every instance of undefined behavior with a warning; but it's probably impossible; at any rate, none do.

Log in or register to write something here or to contact authors.