I\'m looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.
The C++ Standard says it like this:
3.9.1, §2:
There are five signed integer types : "signed char", "short int", "int", "long int", and "long long int". In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment (44); the other signed integer types are provided to meet special needs.
(44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header
<climits>
.
The conclusion: It depends on which architecture you're working on. Any other assumption is false.
For 32-bit systems, the 'de facto' standard is ILP32 — that is, int
, long
and pointer are all 32-bit quantities.
For 64-bit systems, the primary Unix 'de facto' standard is LP64 — long
and pointer are 64-bit (but int
is 32-bit). The Windows 64-bit standard is LLP64 — long long
and pointer are 64-bit (but long
and int
are both 32-bit).
At one time, some Unix systems used an ILP64 organization.
None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.
And, by definition, sizeof(char)
is 1
, notwithstanding the test in the Perl configure script.
Note that there were machines (Crays) where CHAR_BIT
was much larger than 8. That meant, IIRC, that sizeof(int)
was also 1, because both char
and int
were 32-bit.
We are allowed to define a synonym for the type so we can create our own "standard".
On a machine in which sizeof(int) == 4, we can define:
typedef int int32;
int32 i;
int32 j;
...
So when we transfer the code to a different machine where actually the size of long int is 4, we can just redefine the single occurrence of int.
typedef long int int32;
int32 i;
int32 j;
...
As mentioned the size should reflect the current architecture. You could take a peak around in limits.h
if you want to see how your current compiler is handling things.
You can use variables provided by libraries such as OpenGL, Qt, etc.
For example, Qt provides qint8 (guaranteed to be 8-bit on all platforms supported by Qt), qint16, qint32, qint64, quint8, quint16, quint32, quint64, etc.
There is standard.
C90 standard requires that
sizeof(short) <= sizeof(int) <= sizeof(long)
C99 standard requires that
sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
Here is the C99 specifications. Page 22 details sizes of different integral types.
Here is the int type sizes (bits) for Windows platforms:
Type C99 Minimum Windows 32bit
char 8 8
short 16 16
int 16 32
long 32 32
long long 64 64
If you are concerned with portability, or you want the name of the type reflects the size, you can look at the header <inttypes.h>
, where the following macros are available:
int8_t
int16_t
int32_t
int64_t
int8_t
is guaranteed to be 8 bits, and int16_t
is guaranteed to be 16 bits, etc.