Linux-Hams archive - July 1998: sizeof()

sizeof()

Mike Bilow (EEST)
Mon, 06 Jul 98 22:10:00 -0000


Kai Schulte wrote in a message to Mike Bilow:

> 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.

KS> Does that mean "never use more than the low 16 bits (or
KS> maybe even 8) of an integer" or rather "never use the type
KS> int at all"?

It means that you should never write code that will break if it compiles on a
machine with a different size for "int", since this changes frequently.

KS> Kai, who would rather use #ifdefs if the program has to be
KS> portable rather than waste memory by default

As I said, if you really need to know how large a value you can store in an
"int", then find out from the official ANSI manifest constants, "INT_MIN" and
"INT_MAX". One way to do this might well be to use conditionals:

#include <limits.h>

#if LONG_MAX > INT_MAX
#define X_IS_LONG
long x;
#else
#define X_IS_INT
int x;
#endif

The problem is that this approach saves memory at the cost of complexity. For
example, you would later need to worry about the type of x when you print,
since the compiler would not check this for you:

#ifdef X_IS_LONG
printf("x is %ld", x);
#else
printf("x is %d", x);
#endif

Another approach, which is far better, is to use some synthetic types created
with "typedef" when you really need to care about bit-size, such as:

#include <limits.h>

#if LONG_MAX > INT_MAX
typedef long int32;
#else
typedef int int32;
#endif

int32 x;

This doesn't quite fix the "printf" problem, at least not gracefully, but at
least it encapsulates all of the architecture-specific stuff in once place so
that porting becomes relatively easier.

Finally, keep in mind that "wasting" memory might be a good idea in many cases.
For example, in order to save two bytes of data space by declaring a variable
as 16-bit instead of 32-bit (in Linux), you will incur both a performance
penalty and a one-byte code space prefix penalty for _each_ machine instruction
in code space which accesses that variable location. Even if you only access
the variable in two places in the code, once to write and once to read, you
will have given back the two bytes you saved and lost speed. The design
philosophy of C is that "int" should be a natural size for the machine, and all
compilers are pretty good about picking the right size.

-- Mike