什么是兼容“int”? 输入 32 位和 32 位 C++ 中的 64 位窗口?

发布于 2024-07-27 14:20:02 字数 152 浏览 4 评论 0原文

C++ 中兼容的“int”数据类型是什么,可以在 32 位和 32 位上将自身大小调整为 4 个字节? 64 位 Windows 上是 8 个字节?

虽然 INT_PTR 工作得很好,但它降低了可读性,并且它的描述告诉我们将它用于指针运算。

谢谢

What is the compatible "int" datatype in C++ that can resize itself to 4 bytes on 32bit & 8 bytes on 64bit windows?

Although INT_PTR works fine but it reduces the readability as well as its description tells us to use it for pointer arithmetic.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呢古 2024-08-03 14:20:02

如果您正在寻找标准的东西,那么您就不走运了。 该标准没有指定任何内置数据类型的大小。

请注意,INT_PTR 并不意味着指针算术。 我的意思是该类型的大小与 void * 相同,这正是您想要的。 但它并不适用于所有平台(我很确定它是 Windows 特定的)。

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. I means that the type will have the same size as void *, which is exactly what you want. It won't work on all platforms though (I'm pretty sure it's Windows specific).

情深缘浅 2024-08-03 14:20:02

在 Visual Studio 下,您还提供 __int3264 ,它的作用与 INT_PTR 大致相同...

under Visual Studio you are also offered __int3264 which does much the same as INT_PTR ...

遗失的美好 2024-08-03 14:20:02

该标准没有提到具体的大小要求,只是每个整型类型必须提供至少与其之前的类型一样多的存储空间。 因此,int 必须容纳与 short 一样多的内容,依此类推。 您最好指定您需要它们的用途。

如果您正在寻找不会根据操作环境改变大小的整数,请查看 Boost 整数库,或 C99/C++11 标头 。 其中包含 uint32_t/int32_tuintmax_t/intmax_t 等类型。

最重要的是,根据您的问题,它有:uintptr_t/intptr_t。 这些保证具有正确的尺寸以将指针固定在您的平台上。

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. 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>. This contains types such as uint32_t/int32_t and uintmax_t/intmax_t.

Most importantly, based off your question, it has: uintptr_t/intptr_t. These are guaranteed to have the correct size to hold a pointer on your platform.

攒眉千度 2024-08-03 14:20:02

这实际上取决于编译器。 我认为唯一(或多或少)可靠的方法是使用像 (void *) 这样的指针类型。

我认为最好的方法是在头文件中使用一些条件处理并设置自定义类型:(

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

此示例适用于 Visual C++)

It really depends on the compiler. I think the only (more or less) reliable way is by using a pointer type like (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++)

老旧海报 2024-08-03 14:20:02

这可能对您有帮助: http://lists.debian.org/debian -user/2006/04/msg00681.html。 不幸的是,你的问题似乎依赖于编译器。

This might help you: http://lists.debian.org/debian-user/2006/04/msg00681.html. Unfortunatly your question seems to be compiler dependant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文