Size of long integer may be different between 64-bit systems (each expressed as a long integer)
I don't know if I am too outdated...
I have always thought that the short, long, and long data types under C/C ++ are fixed to 2, 4, and 8 bytes.
Size. Only the int type changes to the size of 2 or 4 bytes because of the 16-bit/32-bit system.
The 64-bit power supply should also be changed to 8 bytes (64-bit) size ?!
Sort out the previous article bypass the 2 GB file size limit on 32-bit Linux
In the 64-bit system, the length of long is also expressed by each other!
First, even if the int size is 64-bit, most systems still use 4 bytes, this is mainly to avoid modifying too many places for programs from 32-bit to 64-bit systems.
For more information, see Wikipedia: 64-bit data models.
Description
Most Unix systems use lp64 in 64-Bit mode.
In this data model, long is no longer fixed to 4 bytes, but changed to 8 bytes!
However, win64 uses the data model llp64 instead of lp64, and the long size is still 4 bytes.
Bytes 64-bit compilers today useLp64
Model (including Solaris, Aix, HP, Linux, Mac OS x, and ibm z/OS native compilers). Microsoft's VC ++ compiler usesLlp64
Model.
The maximum difference between the two data models is the size of the long data model. The lp64 value is 64-bit, while the llp64 value is 32-bit.
Llp64 Data Model is basically the same as a 32-bit system. The only difference is that only the address (pointer) is changed to 64-bit.
. The size of a resource object (class, structure) is the same as that of a 32-bit system if there are no members including pointer!
In lp64, apart from changing the address (pointer) to 64-bit, the size of Long is also changed to 64-bit. Therefore, in UNIX, it may take a little more effort to porting the 32-bit program to 64-bit than windows.
Therefore, we have noticed two problems that affect program compatibility.
- In UNIX, the size of Long is different in 32-bit and 64-bit systems.
- The same is true for 64-bit systems. UNIX and windows have different views on the size of long.
To improve the compatibility between 32-bit and 64-bit programs and between UNIX and windows, switching to a fixed-length Database type is a good choice for programming.
In UNIX, we can use stdint. h instead.
The definition of this header file for material types:
int8_t 8-bit signed interger
int16_t 16-bit signed interger
int32_t 32-bit signed interger
int64_t 64-bit signed interger
uint8_t 8-bit unsigned interger
uint16_t 16-bit unsigned interger
uint32_t 32-bit unsigned interger
uint64_t 64-bit unsigned interger
In Windows, replace the following fixed-size Data Types
INT8 8-bit signed integer
INT16 16-bit signed integer
INT32 32-bit signed integer
INT64 64-bit signed integer
UINT8 8-bit unsigned integer
UINT16 16-bit unsigned integer
UINT32 32-bit unsigned integer
UINT64 64-bit unsigned integer
Never use int and long again!
Especially when accessing a web program, it is likely that the client is windows and the server is UNIX, and then there are 32-bit and 64-bit systems mixed in, an incompatible problem occurs when you are not careful...
Of course, writing a program in a 64-bit system is not only the basic information type above. In addition to changing the pointer size to 64-bit
In addition, the size of size_t and off_t used by many built-in function libraries has also changed to 64-bit ....
When writing a program, if these data types are used, you need to pay special attention to them. Especially in the case of casting, do not use a 32-bit integer to consume these data, so as not to cause the result of the unrecoverable period!
Finally, a small program is provided to show you the size of your system's primary information type.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h> #include <sys/types.h> int main() { printf("sizeof(short) = %d/n", sizeof(short)); printf("sizeof(int) = %d/n", sizeof(int)); printf("sizeof(long) = %d/n", sizeof(long)); printf("sizeof(long long) = %d/n/n", sizeof(long long)); printf("sizeof(size_t) = %d/n", sizeof(size_t)); printf("sizeof(off_t) = %d/n", sizeof(off_t)); printf("sizeof(void *) = %d/n", sizeof(void *)); } |
Additional information:
- Wikipedia: 64-Bit Data Models
- 64-bit programming models: Why lp64?
- Introduction to Win32/win64
- Porting 32-bit applications to the itanium Architecture
- Preparing code for the IA-64 Architecture
(PDF)