Originally Posted by
vmanes
What you're asking about is defined (or not specifically defined) in the C++ standards. Size of an int should be the natural size of the achitecture - many of us grew up with 2-byte ints. The standards don't prescribe specifics, it's more on the order of any larger data type must be at least as large as that that comes before it. Thus, today you see that int and long int are generally the same size.
Originally Posted by
Narue
>I have heard that in C++ a char is always 1 byte
Yes. Or to be more specific, "char" and "byte" are synonymous terms, and sizeof(char) is guaranteed to be 1.
>and an integer is always 4 bytes on any machine
Nope. The size of an integer is at least 16 bits, but it can be whatever the implementation chooses as long as the basic requirements for type relations are met.
>its weird that 1 byte can still be 1 byte even if
>the type consists of 16 bits instead of 8 @_@.
It's not weird at all when you understand that "byte" is an abstraction for the smallest addressable unit. An octet (the 8-bit entity you're familiar with) is one such concrete implementation of this abstraction.
>I wanted to see if there was some header or implementation
>that defined the way bytes were mapped for each type
The closest you can get is looking in <limits.h>. CHAR_BIT will tell you how many bits are in a byte, and the *MIN/*MAX values will give you an idea of how other types are structured on your system. You can find out the minimum requirements by looking at a suitable draft of the C standard. Example.
Ah... I think I'm understanding...
So basically, for an octet byte (8 bits) the table would look something like this...
constant integral types:
(using this list as an example)
-char (must be at least 1 byte)
-short (must be at least 1 byte but is typically 2 bytes)
-int (must be at least sizeof(short) bytes but is typically 4 bytes)
-long (must be at least sizeof(int) bytes but is typically 8 bytes)
Ah, so its no wonder int and long have the same range on some machines! O_O
If this is right, this clears some mist XD
Though I have yet to see a machine with a different bit-set that determines a byte. Sorry for my ignorance @_@.
I hope this is the right mindset for this #_#
-Alex