C语言 uint32_t vs int 作为日常编程的约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20077313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:21:11  来源:igfitidea点击:

uint32_t vs int as a convention for everyday programming

cintuint32

提问by Guy

When should one use the datatypes from stdint.h? Is it right to always use as a convention them? What was the purpose of the design of nonspecific size types like int and short?

什么时候应该使用 stdint.h 中的数据类型?总是将它们用作惯例是否正确?设计非特定大小类型(如 int 和 short)的目的是什么?

回答by chux - Reinstate Monica

When should one use the datatypes from stdint.h?

什么时候应该使用 stdint.h 中的数据类型?

  1. When the programming tasks specify the integer width especially to accommodate some file or communication protocol format.
  2. When high degree of portabilitybetween platforms is required over performance.
  1. 当编程任务指定整数宽度时,特别是为了适应某些文件或通信协议格式。
  2. 当需要平台之间的高度可移植性而不是性能时

Is it right to always use as a convention them (then)?

总是将它们用作惯例是否正确(然后)?

Things are leaning that way. The fixed width types are a more recent addition to C. Original C had char, short, int, longand thatwas progressive as it tried, without being toospecific, to accommodate the various integer sizes available across a wide variety of processors and environments. As C is 40ish years old, it speaks to the success of that strategy. Much C code has been written and successfully copes with the soft integer specification size. With increasing needs for consistency, char, short, int, long and long long, are not enough (or at least not so easy) and so int8_t, int16_t, int32_t, int64_tare born. New languages tend to require very specific fixed integer size types and 2's complement. As they are successfully, that Darwinian pressure will push on C. My crystal ball says we will see a slow migration to increasing uses of fixed width types in C.

事情正朝着那个方向发展。固定宽度的类型是一个较新的除了C.原始C具有char, short, int, long是进步的,因为它试图,而不过于特定,以适应各种整数跨各种处理器和环境的尺寸。由于 C 已经 40 岁了,它说明了该策略的成功。已经编写了很多 C 代码并成功地处理了软整数规范大小。随着对一致性的需求不断增加,char, short, int, long and long long, 还不够(或至少不是那么容易)等等int8_t, int16_t, int32_t, int64_t出生。新语言往往需要非常具体的固定整数大小类型和 2 的补码。当它们成功时,达尔文的压力将推动 C。我的水晶球说我们将看到缓慢迁移到 C 中固定宽度类型的增加使用。

What was the purpose of the design of nonspecific size types like int and short?

设计非特定大小类型(如 int 和 short)的目的是什么?

It was a good first step to accommodate the wide variety of various integer widths (8,9,12,18,36, etc.) and encodings (2's, 1's, sign/mag). So much coding today uses power-of-2 size integers with 2's complement, that one may not realize that many other arrangements existed beforehand. See this answeralso.

这是适应各种整数宽度(8、9、12、18、36 等)和编码(2、1、符号/mag)的良好第一步。当今如此多的编码使用 2 次方大小的整数和 2 的补码,以至于人们可能没有意识到之前存在许多其他安排。另请参阅此答案

回答by Nikhil

My work demands that I use them and I actually love using them.

我的工作要求我使用它们,我实际上喜欢使用它们。

I find it useful when I have to implement a protocol and use them inside a structure which can be a message that needs to be sent out or a holder of certain information.

当我必须实现一个协议并在一个结构中使用它们时,我发现它很有用,该结构可以是需要发送的消息或某些信息的持有者。

If I have to use a sequence number that needs to be incremented, I wouldn't use int because sequence numbers aren't supposed to be negative. I use uint32_t instead. I will hence know the sequence number space and can plan/code accordingly.

如果我必须使用需要递增的序列号,我不会使用 int,因为序列号不应该是负数。我使用 uint32_t 代替。因此,我将知道序列号空间并可以相应地计划/编码。

The code we write will be running on 32 as well as 64 bit machine so using "int" on different bit machines results in subtle bugs which can be a pain to identify. Using unint16_t will allocate 16 bits on 32 or 64 bit architecture.

我们编写的代码将在 32 位和 64 位机器上运行,因此在不同位机器上使用“int”会导致细微的错误,这可能很难识别。使用 unint16_t 将在 32 或 64 位架构上分配 16 位。

回答by unwind

No, I would say it's never a good idea to use those for general-purpose programming.

不,我会说将它们用于通用编程从来都不是一个好主意。

If you really care about number of bits, then go ahead and use them but for most general use you don't care so then use the general types. The general types mightbe faster, and they are certainly easier to read and write.

如果您真的关心位数,那么继续使用它们,但对于大多数一般用途,您并不关心,所以请使用一般类型。通用类型可能更快,而且它们肯定更易于阅读和编写。

回答by ensc

Fixed width datatypes should be used only when really required (e.g. when implementing transfer protocols or accessing hardware or requiring a certain range of values (you should use the ..._least_...variant there)). Your program won't adapt else on changed environments (e.g. using uint32_tfor filesizes might be ok 10 years ago, but off_twill adapt to recent needs). As others have pointed out, there might be a performance impact as intmight be faster than uint32_ton 16 bit platforms.

固定宽度数据类型应该只在真正需要时使用(例如,当实现传输协议或访问硬件或需要一定范围的值时(你应该在..._least_...那里使用变体))。您的程序不会适应已更改的环境(例如uint32_t,10 年前用于文件大小可能还可以,但off_t会适应最近的需求)。正如其他人指出的那样,可能会影响性能,因为int可能比uint32_t在 16 位平台上更快。

intitself is very problematic due to its signedness; it is better to use e.g. size_twhen variable holds result of strlen()or sizeof().

int由于它的签名,它本身是非常有问题的;最好使用例如size_t当变量保存strlen()or 的结果时sizeof()