Linux-Hams archive - July 1998: sizeof() is too broken!

sizeof() is too broken!

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


Karl F. Larsen wrote in a message to Mike Bilow:

KFL> I have recieved many responses to my first message all
KFL> saying it is proper for an int to have 4 bytes. So I went
KFL> back to the original function that checks int, float, long
KFL> and char. My GCC sizeof() says all these types use 4 byte!
KFL> This just proves more fully that sizeof() is broken in my
KFL> Linux. Here is the test program.

Read your output more carefully. Here is what I get:

18:41 x:~$ gcc -o karltest karltest.c

18:41 x:~$ ./karltest
Type int has a size of 4 bytes.
Type long has a size of 4 bytes.
Type float has a size of 4 bytes.
Type char has a size of 1 bytes.

18:41 x:~$ cat karltest.c
/* prints out type sizes */
#include <stdio.h>
int main(void)
{
printf("Type int has a size of %d bytes.\n", sizeof(int));
printf("Type long has a size of %d bytes.\n", sizeof(long));
printf("Type float has a size of %d bytes.\n", sizeof(float));
printf("Type char has a size of %d bytes.\n", sizeof(char));
return 0;
}

Look, "sizeof()" works correctly and GCC is not broken. On any 386 or better
Intel machine, 32-bit code likely will have the following natural sizes:

bits bytes

char 8 1
short 16 2
int 32 4
long 32 4
float 32 4
double 64 8
long double 96 12

Wouldn't you think that, if a problem this obvioys really was present in GCC,
someone among the thousands of experienced C programmers who work with Linux
would have noticed it? Consider the possibility that you are simply wrong.

-- Mike