Linux-Hams archive - July 1998: Re: sizeof()

Re: sizeof()

Matthias Welwarsky (terhi.victor@logonet.com)
Mon, 06 Jul 1998 18:39:38 +0200


Kai Schulte wrote:
>
> Mike Bilow wrote:
>
> > In general, it is
> > considered good practice to avoid writing any code which depends upon the
> > underlying implementation size of "int" or any other data type.
>
> Does that mean "never use more than the low 16 bits (or maybe even 8) of
> an integer" or rather "never use the type int at all"?
>
> Kai, who would rather use #ifdefs if the program has to be portable rather
> than waste memory by default

If you deal with hardware, use symbolic types like "uint16_t" meaning 16
bit unsigned. In most cases the range of a type is of no interest
anyway. Don't care about memory that much, care about speed. In most
cases, int defaults to a width that the system can handle fast. Don't
use char as a loop counter even if the range fits, most of the time it
will be slower than int. And of course, don't write code that depends on
the size of simple types like int, short and long. Use typedefs and
provide a types.h depending on the target architecture.

Gruss,
Matthias