Next: , Previous: , Up: Technical notes   [Contents][Index]


28.4 Trap representations

In order to be C89 conformant, an application must initialize all locations that might be read. This is because C89 allows trap representations.

A trap representation is a byte pattern in memory that is not a valid value of some object type. When read, the trap representation causes undefined behavior according to the C89 standard. Because of this undefined behavior the application that allowed the read is non-conformant to the C89 standard. Trap representations are carefully defined and discussed in the C99 standard. See C99

In real life, trap representations can occur when floating point values are used: Some byte patterns that can occur in memory are not valid floating point values, and can cause undefined behavior when read.

Pointers raise the same issue although, since pointers can be safely read as an integer, some insist that an invalid pointer is not, strictly speaking, a trap representation. But there is no portable c89-conformant way of testing the integer form of a pointer for validity, so that the only way to guarantee C89 conformance is to initialize the pointer, either to a valid pointer, or to a known and therefore testable value, such as NULL.

All this implies that, in order to claim c89-conformance, an application must initialize all locations that might be read to non-trap values. For a stack implementation, this means that, as a practical matter, all locations on the stack must be initialized.