I did some research on this topic and I decided to share my findings.
First reason: Standards
On most 32 bits systems type short is 2 bytes long, type int is 4 bytes long. But there are also standards.
According to C90 standard:
sizeof(short) <= sizeof(int) <= sizeof(long)
According to C99 standard:
sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
size will 8 bytes long then there will be no type that it is 4 bytes long
because due to standard, long
type should be at least the same size as int
type. It is possible to make short
type 4 bytes long
About 5 years ago, we moved our main application to 64-bit. And we quickly discovered one pattern that was everywhere and in many different languages: converting pointers to 4-byte integers and back. Here is one of such examples is C++ code:
(void*)(((unsigned int)address) + 1)
It was working in 32-bit systems because pointer and integers are exactly the same size and there is no data loss. But during transition to 64 bits, decision was made to keep integer 4 bytes in size. At least on systems I know of.
As result, if you run code above for address that is greater than max int, you will get invalid address. But until address is in first