请问何时最好/正确使用新的数据类型,即_PTR类型,以便代码可以编译为32位和64位代码? [英] Please when is it best/right to use the new data types i.e the _PTR types so that a code can compile to both 32 and 64 bit code?
本文介绍了请问何时最好/正确使用新的数据类型,即_PTR类型,以便代码可以编译为32位和64位代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问什么时候最好/正确使用新的数据类型,即_PTR类型,以便代码可以编译为32位和64位代码?
我真的很想知道什么时候这样做。请帮忙。我已经阅读了文档但仍然没有得到它。
Please when is it best/right to use the new data types i.e the _PTR types so that a code can compile to both 32 and 64 bit code?
Please I really am at loss as to when to so do.Please help.I have already read documentation and still did not get it.
推荐答案
这回答了一段时间之前在StackOverflow上:
DWORD_PTR,INT_PTR,LONG_PTR,UINT_PTR,ULONG_PTR何时,如何以及为什么? [ ^ ]
他们与32位和64位平台上的单个编译对象运行无关(参考文献中提到的大型数组除外),它们只允许编译器在指针时使用正确大小的存储空间编译为32位或64位平台。源代码不需要改变,编译器做正确的事情。
This was answered some time ago on StackOverflow:
DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR When, How and Why?[^]
They have nothing to do with a single compiled object running on both 32 and 64 bit platforms (except for huge arrays as noted in the reference), they just allow the compiler to use the correct size storage for pointers when compiling for 32 bit or 64 bit platforms. The source code doesn't need to change, the compiler does "the right thing".
所有新的* _ PTR
类型用于处理那些以前不正确使用整数类型的Windows函数,例如LONG
或DWORD
相反,但意味着将这些值用作指针,数组下标或内存偏移值。这些定义在32位系统中没有引起问题,但无法在64位系统上编译或运行,因为64位中的指针或指针差异无法适合DWORD
或LONG
。
技术上,INT_PTR
和LONG_PTR
与其他* _ PTR
类型的不同之处在于它们可以保留负值。这两种类型相当于Microsoft类型SSIZE_T
;或C ++标准类型std :: ptrdiff_t
。对于可用于存储两个内存位置之间差异的任何变量,或者一个对象的大小与内存中另一个对象的大小之间的差异,您需要该类型。请注意,在常见程序中,非常非常罕见,需要这种类型的变量。
UINT_PTR
和ULONG_PTR
似乎适用于需要数组下标,内存基址的偏移量或内存中对象大小的用途。 Microsoft类型SIZE_T
和C ++标准类型std :: size_t
的定义完全相同,并填写相同的角色。
DWORD_PTR
似乎是唯一新的* PTR
履行尾随'PTR
'隐含的角色的类型:即存储实际指针。
你完全没有理由使用这些* _ PTR
类型,因为它们等同于标准指针,分别为std :: size_t
和std :: ptrdiff_t
。
All the new*_PTR
types are meant to be used for dealing with those windows functions that formerly, incorrectly, used integer types such asLONG
orDWORD
instead, but meant to use those values as pointers, array subscripts, or memory offset values. These definitions didn't cause a problem in 32 bit systems, but fail to compile or run on 64 bit systems, because pointers or pointer differences in 64 bit can't fit into aDWORD
orLONG
.
Technically,INT_PTR
andLONG_PTR
are different from the other*_PTR
types in that they can hold negative values. These two types are equivalent to the Microsoft typeSSIZE_T
; or the C++ standard typestd::ptrdiff_t
. You need that type for any variable that may be used to store the difference between two memory locations, or the difference between the size of one object and the size of another object in memory. Note that it is very, very rare in common programs to ever, truly, need a variable of this type.
UINT_PTR
andULONG_PTR
appear to be meant for uses where you need an array subscript, an offset to a memory base address, or the size of an object in memory. The Microsoft typeSIZE_T
, and the C++ standard typestd::size_t
are defined exactly the same, and fill the same role.
DWORD_PTR
appears to be the only of the new*PTR
types that fulfill the role implied by the trailing 'PTR
': that is, store an actual pointer.
There is no reason at all for you to use any of these*_PTR
types, as they are equivalent to standard pointers,std::size_t
, andstd::ptrdiff_t
respectively.
这篇关于请问何时最好/正确使用新的数据类型,即_PTR类型,以便代码可以编译为32位和64位代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文