Portabilidade: O tamanho de um byte (ISO 9989)

Sempre achei que o símbolo CHAR_BIT, definido em limits.h, fosse apenas o tamanho, em bits, do tipo char. Well… estava enganado. A sessão 6.2.6.1.4 da ISO 9989:1999 (em diante) nos diz que:

“Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value. Values stored in bit-fields consist of m bits, where m is the size specified for the bit-field. The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it. Tw o values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations.”

Note a frase inicial: “Valores armazenados em objetos não bit-field de qualquer objeto consistem em n*CHAR_BIT bits”.

Isso é interessante, já que, em arquiteturas antigas, 1 “byte” podia ter tamanho diferente de 8 bits.

Fica a dica…