c++32位系统和64位系统中类型的字节区别

TypeType's size on x32 / x64 platformNote
int32 / 32Basic type. On 64-bit systems remains 32-bit.
long32 / 32Basic type. On 64-bit Windows systems remains 32-bit. Keep in mind that in 64-bit Linux systems this type was extended to 64-bit. Don't forget about it if you develop code which should be compiled for Windows and Linux systems.
size_t32 / 64Basic unsigned type. The type's size is chosen in such a way that you could write the maximum size of a theoretically possible array into it. You can safely put a pointer into size_t type (except for pointers to class functions, but this is a special case).
ptrdiff_t32 / 64Similar to size_t type but this is a signed type. The result of the expression where one pointer is subtracted from the other (ptr1-ptr2) will have ptrdiff_t type.
Pointer32 / 64The size of the pointer directly depends on the platform's size. Be careful while converting pointers to other types.
__int6464 / 64Signed 64-bit type.
DWORD32 / 3232-bit unsigned type. In WinDef.h is defined as:typedef unsigned long DWORD;
DWORDLONG64 / 6464-bit unsigned type. In WinNT.h is defined as:typedef ULONGLONG DWORDLONG;
DWORD_PTR32 / 64Unsigned type in which a pointer can be placed. In BaseTsd.h is defined as:typedef ULONG_PTR DWORD_PTR;
DWORD3232 / 3232-bit unsigned type. In BaseTsd.h is defined as:typedef unsigned int DWORD32;
DWORD6464 / 6464-bit unsigned type. In BaseTsd.h is defined as:typedef unsigned __int64 DWORD64;
HALF_PTR16 / 32A half of a pointer. In Basetsd.h is defined as:#ifdef _WIN64 typedef int HALF_PTR;#else typedef short HALF_PTR;#endif
INT_PTR32 / 64Signed type in which a pointer can be placed. In BaseTsd.h is defined as:#if defined(_WIN64) typedef __int64 INT_PTR; #else typedef int INT_PTR;#endif
LONG32 / 32Signed type which remained 32-bit. That's why in many cases LONG_PTR now should be used. In WinNT.h is defined as:typedef long LONG;
LONG_PTR32 / 64Signed type in which a pointer can be placed. In BaseTsd.h is defined as:#if defined(_WIN64) typedef __int64 LONG_PTR; #else typedef long LONG_PTR;#endif
LPARAM32 / 64Parameter for sending messages. In WinNT.h is defined as:typedef LONG_PTR LPARAM;
SIZE_T32 / 64Analog of size_t type. In BaseTsd.h is defined as:typedef ULONG_PTR SIZE_T;
SSIZE_T32 / 64Analog of ptrdiff_t type. In BaseTsd.h is defined as:typedef LONG_PTR SSIZE_T;
ULONG_PTR32 / 64Unsigned type in which a pointer can be placed. In BaseTsd.h is defined as:#if defined(_WIN64) typedef unsigned __int64 ULONG_PTR;#else typedef unsigned long ULONG_PTR;#endif
WORD16 / 16Unsigned 16-bit type. In WinDef.h is defined as:typedef unsigned short WORD;
WPARAM32 / 64Parameter for sending messages. In WinDef.h is defined as:typedef UINT_PTR WPARAM;

中文版

类型  32位 64位

int    32    32

char 8     8

long 32  32

指针 32  64 

例题:

64位操作系统,不同类型变量对应的字节数为:(红色的表示与32位系统不同之处)
        char :1个字节  
      char*(即指针变量): 8个字节 
      short int : 2个字节  
      int:  4个字节  
      unsigned int : 4个字节  
      float:  4个字节  
      double:    8个字节  
      long:   8个字节 
      long long:  8个字节  
      unsigned long:  8个字节 

64位系统在内存管理方面遵循8字节对齐,原则:在8字节对齐的情况下,按8个字节为单位分配存储空间,如果不足,会自动补充,本次分配不足以存放下面的变量时,会重新分配空间。
structA{
   unsigned int a; //对于开始连续的第一个8字节内存,a占4字节 
   char b[2];      //b[]占1*2字节,在a后的连续2字节内存放,还剩2个字节,
   double c;       //c占8字节,第一个8字节还剩2字节不足以存放c,自动补充该2字节。并同时开辟第二个8字节内存存放c
   short d;        //d占2字节,开辟第三个8字节,存放d。剩余的6个字节补齐。故总共开辟了8*3=24字节存放该数据结构
}





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值