64位与32位编程的数据类型区别(C/C++) (主要介绍windows)
2012-10-25 10:09
501 查看
转自: http://zxjgoodboy.i.sohu.com/blog/view/136343091.htm
Common Visual C++ 64-bit Migration Issues
When you use Visual C++ to create applications to run on a 64-bit Windows operating system, you should be aware of the following issues:
An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on
64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.
size_t, time_t, and ptrdiff_t are 64-bit values on 64-bit Windows operating systems.
time_t is a 32-bit value on 32-bit Windows operating systems in Visual C++ versions before Visual C++ 2005. In Visual C++ 2005 and later,
time_t is a 64-bit integer by default. For more information, see
Time Management.
You should be aware of where your code takes an int value and processes it as a
size_t or time_t value. It is possible that the number could grow to be larger than a 32-bit number and data will be truncated when it is passed back to the
int storage.
The %x (hex int format) printf modifier will not work as expected on a 64-bit Windows operating system. It will only operate on the first 32 bits of the value that is passed to it.
Use %I32x to display an integer on a Windows 32-bit operating system.
Use %I64x to display an integer on a Windows 64-bit operating system.
The %p (hex format for a pointer) will work as expected on a 64-bit Windows operating system.
64位与32位编程的数据类型区别
C/C++仅仅定义了这些基本数据类型之间的关系,并没有定义严格定义它们的字长。在不同的平台上,根据编译器不同的实现,它们的字长如下表所示:
在这张表中,LP64,ILP64,LLP64是64位平台上的字长模型,ILP32和LP32是32位平台上的字长模型。
LP64意思是long和pointer是64位,ILP64指int,long,pointer是64位,LLP指long long和pointer是32-bit的。ILP32指int,long和pointer是32位的,LP32指long和pointer是32位的。
32位Windows采用的是LP32数据模型,64位Windows采用的是LLP64数据模型。
所以,Windows上的32位程序设计和64位程序设计最大的不同(也就是IP32和LLP64的不同),就在于指针的长度不同??由32位变成了64位。
Win32 API在很多情况下,都需要将整数转换成指针或者相反。在 32 位的硬件上不会有问题,其中指针的大小和整数的大小是相同的,但在 64 位的硬件上却完全不一样。
为此M$搞了个所谓的“多态类型”:
对于特定的精度,您可以使用固定精度的数据类型。不管处理器的词大小如何,它们的大小都是一致的。大多数这些类型都在它们的名称中包含精度,可以从下面的表中看出:
此外,当您需要数据类型的精度随着处理器词大小变化时,请使用指针精度数据类型。这些类型又称为“多态”数据类型。这些类型通常以 _PTR 后缀结尾,如下面的表格所示:
通过整数参数传递参数或上下文信息的所有 Win32 API 都更改为使用这些新的类型。
此外,还出现了定长指针:POINTER_32和POINTER_64:
#define POINTER_32 __ptr32
#define POINTER_64 __ptr64
PS:M$DN中说是在Basetsd.h中定义的,但实际上是在WinNT.h中定义的。
Common Visual C++ 64-bit Migration Issues
When you use Visual C++ to create applications to run on a 64-bit Windows operating system, you should be aware of the following issues:
An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on
64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.
size_t, time_t, and ptrdiff_t are 64-bit values on 64-bit Windows operating systems.
time_t is a 32-bit value on 32-bit Windows operating systems in Visual C++ versions before Visual C++ 2005. In Visual C++ 2005 and later,
time_t is a 64-bit integer by default. For more information, see
Time Management.
You should be aware of where your code takes an int value and processes it as a
size_t or time_t value. It is possible that the number could grow to be larger than a 32-bit number and data will be truncated when it is passed back to the
int storage.
The %x (hex int format) printf modifier will not work as expected on a 64-bit Windows operating system. It will only operate on the first 32 bits of the value that is passed to it.
Use %I32x to display an integer on a Windows 32-bit operating system.
Use %I64x to display an integer on a Windows 64-bit operating system.
The %p (hex format for a pointer) will work as expected on a 64-bit Windows operating system.
64位与32位编程的数据类型区别
C/C++仅仅定义了这些基本数据类型之间的关系,并没有定义严格定义它们的字长。在不同的平台上,根据编译器不同的实现,它们的字长如下表所示:
数据类型 | LP64 | ILP64 | LLP64 | ILP32 | LP32 |
char | 8 | 8 | 8 | 8 | 8 |
short | 16 | 16 | 16 | 16 | 16 |
_int32 | N/A | 32 | N/A | N/A | N/A |
int | 32 | 64 | 32 | 32 | 16 |
long | 64 | 64 | 32 | 32 | 32 |
long long | N/A | N/A | 64 | N/A | N/A |
pointer | 64 | 64 | 64 | 32 | 32 |
LP64意思是long和pointer是64位,ILP64指int,long,pointer是64位,LLP指long long和pointer是32-bit的。ILP32指int,long和pointer是32位的,LP32指long和pointer是32位的。
32位Windows采用的是LP32数据模型,64位Windows采用的是LLP64数据模型。
所以,Windows上的32位程序设计和64位程序设计最大的不同(也就是IP32和LLP64的不同),就在于指针的长度不同??由32位变成了64位。
Win32 API在很多情况下,都需要将整数转换成指针或者相反。在 32 位的硬件上不会有问题,其中指针的大小和整数的大小是相同的,但在 64 位的硬件上却完全不一样。
为此M$搞了个所谓的“多态类型”:
对于特定的精度,您可以使用固定精度的数据类型。不管处理器的词大小如何,它们的大小都是一致的。大多数这些类型都在它们的名称中包含精度,可以从下面的表中看出:
表 1. 固定精度的数据类型 | |
类型 | 定义 |
DWORD32 | 32 位无符号整数 |
DWORD64 | 64 位无符号整数 |
INT32 | 32 位有符号整数 |
INT64 | 64 位有符号整数 |
LONG32 | 32 位有符号整数 |
LONG64 | 64 位有符号整数 |
UINT32 | 无符号 INT32 |
UINT64 | 无符号 INT64 |
ULONG32 | 无符号 LONG32 |
ULONG64 | 无符号 LONG64 |
表 2. 指针精度的数据类型 | |
类型 | 定义 |
DWORD_PTR | 指针精度的无符号长类型 |
HALF_PTR | 指针大小的一半。用于包含一个指针和两个小型字段的结构中 |
INT_PTR | 指针精度的有符号整型 |
LONG_PTR | 指针精度的有符号长类型 |
SIZE_T | 指针可以引用的最大字节数。用于必须跨指针的整个范围的计数 |
SSIZE_T | 有符号 SIZE_T |
UHALF_PTR | 无符号 HALF_PTR |
UINT_PTR | 无符号 INT_PTR |
ULONG_PTR | 无符号 LONG_PTR |
LPARAM | 与 LONG_PTR 为同义词,(在WTypes.h 中定义) |
WPARAM | 与 UINT_PTR 为同义词,(在 WTypes.h 中定义) |
此外,还出现了定长指针:POINTER_32和POINTER_64:
#define POINTER_32 __ptr32
#define POINTER_64 __ptr64
PS:M$DN中说是在Basetsd.h中定义的,但实际上是在WinNT.h中定义的。
相关文章推荐
- 64位与32位编程的数据类型区别(C/C++)
- 64位与32位编程的数据类型区别
- 64位与32位编程的数据类型区别
- 32位和64位编程的数据类型长度区别
- 64位与32位编程的数据类型区别(C/C++)
- 64位与32位编程的数据类型区别(C/C++)
- 64位与32位编程的数据类型区别
- 64位与32位编程的数据类型区别(C/C++)
- 64位与32位编程的数据类型区别
- 64位与32位编程的数据类型区别(C/C++)
- 64位与32位编程的数据类型区别
- 64位与32位编程的数据类型区别
- 32位和64位系统区别及常用数据类型所占字节数
- 64位与32位编程的数据类型
- 关于Windows与Linux下32位与64位开发中的数据类型长度的一点汇总
- 不同的数据类型在32位和64位下所占字节的区别
- 64位和32位编译环境下不同数据类型长度的区别
- 关于Windows与Linux下32位与64位开发中的数据类型长度的一点汇总
- 关于Windows与Linux下32位与64位开发中的数据类型长度的一点汇总
- 编译器32位和64位数据类型区别