Generally, I’m rather finicky about making assumptions about the sizes of types, and even conversions between signed and unsigned types. Although I occasionally skirt dangerous gronud, such as feeding a function pointer into a object pointer, and expect to be able to cast the void* back to the function pointer (basically implementation defined by C, but required by POSIX), I also tend to make notes of when (fully aware) I do things that are non portable, but not necessarily obvious. At least in the example case I just mentioned, I didn’t know that was dangerous ground until I reviewed code under -pendantic, and scratched my head at the required warning message.
Normally I take things in stride, and just cringe when I see, “Portable” software doing stupid things like using unsigned int where they mean uint32_t, or the (flawed) assumption that a pointer to xyz will be as large as an object of xyz. So I thought I’d just take a look see here, and then wrote a program to display it in bits rather then bytes, since most of the folks I know will better get the picture that way :-o.
Also being a practical man, I kind of like to know what is off the edge of the map, just in case I some day have to jump off o/.
Here is a simple program to solve my curiosity:
#include
#include
#include
#include
int
main(void) {
printf("sizeof(char)t= %u-bitsn", sizeof(char)*CHAR_BIT);
printf("sizeof(char*)t= %u-bitsn", sizeof(char*)*CHAR_BIT);
printf("sizeof(wchar_t)t= %u-bitsn", sizeof(wchar_t)*CHAR_BIT);
printf("sizeof(wchar_t*)t= %u-bitsn", sizeof(wchar_t*)*CHAR_BIT);
printf("sizeof(short int)t= %u-bitsn", sizeof(short int)*CHAR_BIT);
printf("sizeof(short int*)t= %u-bitsn", sizeof(short int*)*CHAR_BIT);
printf("sizeof(int)t= %u-bitsn", sizeof(int)*CHAR_BIT);
printf("sizeof(int*)t= %u-bitsn", sizeof(int*)*CHAR_BIT);
printf("sizeof(long)t= %u-bitsn", sizeof(long)*CHAR_BIT);
printf("sizeof(long*)t= %u-bitsn", sizeof(long*)*CHAR_BIT);
printf("sizeof(long long)t= %u-bitsn", sizeof(long long)*CHAR_BIT);
printf("sizeof(long long*)t= %u-bitsn", sizeof(long long*)*CHAR_BIT);
printf("sizeof(size_t)t= %u-bitsn", sizeof(size_t)*CHAR_BIT);
printf("sizeof(size_t*)t= %u-bitsn", sizeof(size_t*)*CHAR_BIT);
printf("sizeof(float)t= %u-bitsn", sizeof(float)*CHAR_BIT);
printf("sizeof(float*)t= %u-bitsn", sizeof(float*)*CHAR_BIT);
printf("sizeof(double)t= %u-bitsn", sizeof(double)*CHAR_BIT);
printf("sizeof(double*)t= %u-bitsn", sizeof(double*)*CHAR_BIT);
printf("sizeof(long double)t= %u-bitsn", sizeof(long double)*CHAR_BIT);
printf("sizeof(long double*)t= %u-bitsn", sizeof(long double*)*CHAR_BIT);
printf("sizeof(ptrdiff_t)t= %u-bitsn", sizeof(ptrdiff_t)*CHAR_BIT);
printf("sizeof(ptrdiff_t*)t= %u-bitsn", sizeof(ptrdiff_t*)*CHAR_BIT);
printf("sizeof(intptr_t)t= %u-bitsn", sizeof(intptr_t)*CHAR_BIT);
printf("sizeof(intptr_t*)t= %u-bitsn", sizeof(intptr_t*)*CHAR_BIT);
return 0;
}
The C standard defines CHAR_BIT in limits.h, as being the number of bits for the smallest object that is not a bit field, roughly meaning that CHAR_BIT = number of bits in a byte, for all practical intents and purposes. Like wise, the sizeof operator is defined as returning the size of its operand in bytes, as an implementation defined unsigned integer value having the type size_t, from stddef.h. For the fuckos out there, the standard also says that a char object is large enough to store any character of the basic execution set (A-Z, a-z, 0-9, space, plus the required punctuation and control characters—roughly a character set of 99 symbols that fit within a single byte), and that those characters will have a non negative value while doing it. It also declares that sizeof(char) == 1. From this we can infer that sizeof(x) * CHAR_BIT should be the size of x in bits, and that ‘x’ is basically as good as off the edge of the map, for any x that you can’t make on my grandmothers type writer.
Having the size of each type followed by a pointer to it displayed, is mostly done to emphasis that a pointer to a size, means dick all about the size of the pointer. You’ll notice an interesting connection between pointer size and your hardware however. Gee, that just doesn’t sound right, LOL.
Some examples:
Run on FreeBSD 8.0-STABLE i386:
sizeof(char) = 8-bits
sizeof(char*) = 32-bits
sizeof(wchar_t) = 32-bits
sizeof(wchar_t*) = 32-bits
sizeof(short int) = 16-bits
sizeof(short int*) = 32-bits
sizeof(int) = 32-bits
sizeof(int*) = 32-bits
sizeof(long) = 32-bits
sizeof(long*) = 32-bits
sizeof(long long) = 64-bits
sizeof(long long*) = 32-bits
sizeof(size_t) = 32-bits
sizeof(size_t*) = 32-bits
sizeof(float) = 32-bits
sizeof(float*) = 32-bits
sizeof(double) = 64-bits
sizeof(double*) = 32-bits
sizeof(long double) = 96-bits
sizeof(long double*) = 32-bits
sizeof(ptrdiff_t) = 32-bits
sizeof(ptrdiff_t*) = 32-bits
sizeof(intptr_t) = 32-bits
sizeof(intptr_t*) = 32-bits
and FreeBSD 8.0-RELEASE amd64:
sizeof(char) = 8-bits
sizeof(char*) = 64-bits
sizeof(wchar_t) = 32-bits
sizeof(wchar_t*) = 64-bits
sizeof(short int) = 16-bits
sizeof(short int*) = 64-bits
sizeof(int) = 32-bits
sizeof(int*) = 64-bits
sizeof(long) = 64-bits
sizeof(long*) = 64-bits
sizeof(long long) = 64-bits
sizeof(long long*) = 64-bits
sizeof(size_t) = 64-bits
sizeof(size_t*) = 64-bits
sizeof(float) = 32-bits
sizeof(float*) = 64-bits
sizeof(double) = 64-bits
sizeof(double*) = 64-bits
sizeof(long double) = 128-bits
sizeof(long double*) = 64-bits
sizeof(ptrdiff_t) = 64-bits
sizeof(ptrdiff_t*) = 64-bits
sizeof(intptr_t) = 64-bits
sizeof(intptr_t*) = 64-bits
I also have access to 32-bit versions of Windows NT and OpenBSD running on Pentium 4-grade hardware, but don’t feel like booting the wintel tonight, I’m to comfortable with Dixie hehe. Perhaps I will run the program on other systems and implementations, for the sake of testing, and add it to this entry as a comment.