bitwise copying and trapping values

In C, unsigned char is the only type guaranteed to have no trapping values, and which guarantees copying will result in an exact bitwise image. (C++ extends this guarantee to char as well.)

Can somebody please elaborate and explain the above sentence in good detail.
Thanks !

Last edited on
The object representation of an object of some type T is the sequence of sizeof(T) bytes occupied by the object
(with appropriate alignment).

The value representation of an object of some type T is the subset of bits in its object representation that determines the value of the object. (All possible bit patterns in that subset need not be valid value representations.)

For instance, for the type bool, the object representation is sizeof(bool) bytes (typically one byte).
The value representation may be just one bit in that byte.

For the types char, signed char and unsigned char,
all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.


1
2
3
4
5
6
7
8
9
10
char a = '!' ;
char b = a ; // both 'a' and 'b' have identical bits in their object representation

unsigned char u ; // 'u' has a valid object representation (and a valid value representation that holds a number)

bool c = true ;
bool d = c ; // 'c' and 'd' need not have identical bits in their object representation
             // though the bit(s) that make(s) up their value representations would be identical.

unsigned int e ;  // 'e' need not have a valid value representation that holds a number 


I don't know C very well, but AFAIK, it is the same in both C and C++.
Last edited on
Topic archived. No new replies allowed.