简体   繁体   English

什么是C ++中的32Bit和64Bit窗口中的兼容“int”类型?

[英]What is Compatible “int” type in both 32Bit & 64Bit windows in C++?

What is the compatible "int" datatype in C++ that can resize itself to 4 bytes on 32bit & 8 bytes on 64bit windows? C ++中兼容的“int”数据类型是什么,它可以在32位上调整为4个字节,在64位窗口上调整为8个字节?

Although INT_PTR works fine but it reduces the readability as well as its description tells us to use it for pointer arithmetic. 虽然INT_PTR工作正常,但它降低了可读性,并且它的描述告诉我们将它用于指针算术。

Thanks 谢谢

If you're looking for something standard, you're out of luck. 如果你正在寻找标准的东西,那你就不走运了。 The standard does not specify the size of any of the built-in datatypes. 该标准未指定任何内置数据类型的大小。

Note, that INT_PTR does not imply pointer arithmetic. 注意, INT_PTR并不意味着指针算术。 I means that the type will have the same size as void * , which is exactly what you want. 我的意思是该类型将具有与void *相同的大小,这正是您想要的。 It won't work on all platforms though (I'm pretty sure it's Windows specific). 它不适用于所有平台(我很确定它是Windows特定的)。

The standard does not mention specific size requirements, only that each integral type must provide at least as much storage as the type before it. 该标准没有提到具体的尺寸要求,只是每个整体类型必须提供至少与之前类型相同的存储量。 So int must hold as much as a short , and so on. 所以int必须保持short ,等等。 You're better off specifying what you need them for. 你最好指定一下你需要它们。

If you're looking for integers that do not change size based on the operating environment, take a look at the Boost Integer Library , or the C99/C++11 header <cstdint> . 如果您正在寻找不会根据操作环境更改大小的整数,请查看Boost Integer Library或C99 / C ++ 11标头<cstdint> This contains types such as uint32_t / int32_t and uintmax_t / intmax_t . 它包含诸如uint32_t / int32_tuintmax_t / intmax_t类的类型。

Most importantly, based off your question, it has: uintptr_t / intptr_t . 最重要的是,根据您的问题,它有: uintptr_t / intptr_t These are guaranteed to have the correct size to hold a pointer on your platform. 保证它们具有正确的大小,以便在您的平台上保持指针。

在Visual Studio下,您还可以获得__int3264,其功能与INT_PTR相同...

It really depends on the compiler. 这真的取决于编译器。 I think the only (more or less) reliable way is by using a pointer type like (void *). 我认为唯一(或多或少)可靠的方法是使用像(void *)这样的指针类型。

I think the best way is by using some conditional processing in your header file and set a custom type: 我认为最好的方法是在头文件中使用一些条件处理并设置自定义类型:

#ifdef _WIN64
  typedef __int64 NATIVEINT;
#else
  typedef __int32 NATIVEINT;
#endif

(this sample is for Visual C++) (此示例适用于Visual C ++)

This might help you: http://lists.debian.org/debian-user/2006/04/msg00681.html . 这可能对您有所帮助: http//lists.debian.org/debian-user/2006/04/msg00681.html Unfortunatly your question seems to be compiler dependant. 不幸的是,你的问题似乎依赖于编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM