修复了 Linux 内核中的 64 位整数

Fixed 64 bit integer in Linux Kernel

我想在 linux 内核中有一个固定大小的 64 位整数。

我的问题:

  1. 如果我使用 unsigned long,那么它可能在一种架构上是 64 位的,而在另一种架构上是 32 位的。正确的?
  2. 固定的数据类型是什么 64 位整数?

我特指linux内核

If I use unsigned long, then it may be 64-bit on one architecture and 32-bit on another. Right?

是的,unsigned long 的大小是实现定义的,因此不可移植。

What would be the data type the for fixed 64-bit integer?

int64_tuint64_t.

C语言提供了很多固定宽度的类型,有符号的和无符号的。

正如伦丁所说,"the size of unsigned long is implementation-defined and therefore non-portable"。

因此,intunsigned intfloat 等常见类型取决于您的编译器。

您可以查看自 C99 以来可用的固定宽度整数类型的完整列表 in this reference

根据文档,

int64_t 是“signed 整数类型,宽度恰好为 64 位,没有填充位,负值使用 2 的补码”。

uint64_t,同样根据文档,一个“无符号整数类型,宽度恰好为 64 位”

但是还有更多其他固定的 64 位宽度整数类型,您可能会好奇地看到,如:uint_fast64_tuint_least64_t

我建议你看看 reference 并从学习神奇的 C 类型中获得很多乐趣\o/

您还可以在 GNU libc manual of integer types

上查看可用的类型

希望对您有所帮助!