对于Windows和Linux,在c++中定义64位整数

时间:2022-02-08 01:59:44

I am trying to write cross-platform code in C++ for Windows(MinGW) and Linux(g++). I was used to define 64 bit integers in Linux as "long", but when I moved to MinGW, the sizeof(long) returned 4 bytes. Then I discovered that I can use "long long" or "__INT64" to define 64 bit integers in MinGW. I have two questions:

我正在尝试用c++为Windows(MinGW)和Linux(g++)编写跨平台代码。我在Linux中定义64位整数为“long”,但是当我移动到MinGW时,sizeof(long)返回4个字节。然后我发现,我可以使用“long long”或“__INT64”来定义MinGW中的64位整数。我有两个问题:

1.-What is the most portable way to define 64 bit integers for both Windows and Linux? I am currently using #ifdef, but I don't know if this is the best way to do it:

1。-为Windows和Linux定义64位整数最方便的方式是什么?我目前正在使用#ifdef,但我不知道这是否是最好的方法:

#ifdef LINUX
    #define INT64 long 
#elif WIN32
    #define INT64 long long
#endif

2.-Should I use "long long" or "__INT64" in MinGW? and why?

2。-我应该用“long long long long”还是“__INT64”?,为什么?

2 个解决方案

#1


14  

You could use the type int64_t, which is defined in the header cstdint. This is standard as of C++11.

您可以使用类型int64_t,它在标题cstdint中定义。这是标准的c++ 11。

Note that this type might not exist if the platform you're using does not support 64 bit integers.

注意,如果您正在使用的平台不支持64位整数,则该类型可能不存在。

As for long long, that is another possibility. long longs are at least 64-bits wide. Note that it is standard as of C++11 as well, even though it will work on several compilers when using C++03.

至于长期,这是另一种可能。长长龙至少有64位宽。注意,它也是c++ 11的标准,尽管在使用c++ 03时,它可以在几个编译器上运行。

As mentioned by Pete Becker, you could use int_least64_t. This is a good choice if you don't mind using exactly 64 bit integers, but some integral type that is at least 64 bits wide.

正如Pete Becker所提到的,您可以使用int_least64_t。如果您不介意使用64位整数,那么这是一个不错的选择,但是有些整数类型至少是64位的。

#2


2  

I believe the question how to get a 64-bit integer was answered sufficiently, but I'll take a shot at the second one, whether to use "long long" or "__INT64". The question you need to answer first is what you need. If you need exactly 64 bits for some reason, by all means use something that gives you exactly those 64 bits, i.e. int64_t. Even if your platform/compiler doesn't have stdint.h, create your typedef as a suitable type as workaround (#ifdef'd for the compiler), because it makes your code more self-documenting. On the other hand, if you are handling e.g. the offsets in large memory segments (like e.g. a DVD image), use size_t or ssize_t, because those are typically the same size as a pointer, which is also self-documenting. Similarly, the offset in a file is best represented using fpos_t.

我相信关于如何获得64位整数的问题已经得到了充分的回答,但是我将尝试第二个问题,是使用“long long long”还是“__INT64”。你首先要回答的问题是你需要什么。如果出于某种原因,你需要64位,那么一定要使用一些能给出64位的东西,比如int64_t。即使您的平台/编译器没有stdint。h,将你的typedef创建为一个合适的类型作为工作区(#ifdef为编译器创建),因为它使你的代码更具有自文档化的特性。另一方面,如果您处理的是大型内存段的偏移量(如DVD镜像),则使用size_t或ssize_t,因为它们的大小通常与指针相同,这也是自文档化的。类似地,文件中的偏移量最好使用fpos_t表示。

BTW: The assumption that "long" is 64 bits in Linux is wrong. In particular on 32-bit variants it is not 64 bits and I'm also not sure if really all 64-bit variants use 64-bit longs. These different systems are called ILP32, LP64 and LLP64. Search those terms on the web!

顺便说一句:假设在Linux中“长”是64位是错误的。特别是对于32位变体,它不是64位的,我也不确定是否所有64位变体都使用64位长字符。这些不同的系统被称为ILP32、LP64和LLP64。在网上搜索这些术语!

#1


14  

You could use the type int64_t, which is defined in the header cstdint. This is standard as of C++11.

您可以使用类型int64_t,它在标题cstdint中定义。这是标准的c++ 11。

Note that this type might not exist if the platform you're using does not support 64 bit integers.

注意,如果您正在使用的平台不支持64位整数,则该类型可能不存在。

As for long long, that is another possibility. long longs are at least 64-bits wide. Note that it is standard as of C++11 as well, even though it will work on several compilers when using C++03.

至于长期,这是另一种可能。长长龙至少有64位宽。注意,它也是c++ 11的标准,尽管在使用c++ 03时,它可以在几个编译器上运行。

As mentioned by Pete Becker, you could use int_least64_t. This is a good choice if you don't mind using exactly 64 bit integers, but some integral type that is at least 64 bits wide.

正如Pete Becker所提到的,您可以使用int_least64_t。如果您不介意使用64位整数,那么这是一个不错的选择,但是有些整数类型至少是64位的。

#2


2  

I believe the question how to get a 64-bit integer was answered sufficiently, but I'll take a shot at the second one, whether to use "long long" or "__INT64". The question you need to answer first is what you need. If you need exactly 64 bits for some reason, by all means use something that gives you exactly those 64 bits, i.e. int64_t. Even if your platform/compiler doesn't have stdint.h, create your typedef as a suitable type as workaround (#ifdef'd for the compiler), because it makes your code more self-documenting. On the other hand, if you are handling e.g. the offsets in large memory segments (like e.g. a DVD image), use size_t or ssize_t, because those are typically the same size as a pointer, which is also self-documenting. Similarly, the offset in a file is best represented using fpos_t.

我相信关于如何获得64位整数的问题已经得到了充分的回答,但是我将尝试第二个问题,是使用“long long long”还是“__INT64”。你首先要回答的问题是你需要什么。如果出于某种原因,你需要64位,那么一定要使用一些能给出64位的东西,比如int64_t。即使您的平台/编译器没有stdint。h,将你的typedef创建为一个合适的类型作为工作区(#ifdef为编译器创建),因为它使你的代码更具有自文档化的特性。另一方面,如果您处理的是大型内存段的偏移量(如DVD镜像),则使用size_t或ssize_t,因为它们的大小通常与指针相同,这也是自文档化的。类似地,文件中的偏移量最好使用fpos_t表示。

BTW: The assumption that "long" is 64 bits in Linux is wrong. In particular on 32-bit variants it is not 64 bits and I'm also not sure if really all 64-bit variants use 64-bit longs. These different systems are called ILP32, LP64 and LLP64. Search those terms on the web!

顺便说一句:假设在Linux中“长”是64位是错误的。特别是对于32位变体,它不是64位的,我也不确定是否所有64位变体都使用64位长字符。这些不同的系统被称为ILP32、LP64和LLP64。在网上搜索这些术语!