选择最合适的整数大小/范围用于变量

发布于 2024-09-27 13:41:30 字数 1113 浏览 3 评论 0原文

C99 中的 stdint.h 提供了许多整数大小、类型的选项和范围 - 太多了我不知道该选择什么!

我知道如何在适当的时候使用size_tptrdiff_t,我使用固定大小类型进行存储和传输。我的问题涉及仅存储在主机内存中的值。

例如,图像的结构可能包含以下成员:

struct image {
    integer width, height; /* pixel dimensions of the image */
    integer bits_per_pixel;
    ...
};

如果 widthheight 永远不会超过 SHRT_MAX,则应该 short<使用 /code>,还是坚持使用 int?图像的宽度或高度不能为负,因此使用无符号类型?也许 (u)int_least16_t 是正确的选择?还有别的事吗?

如果 bits_per_pixel 永远不会超过值 64,请使用 charunsigned charuint8_tint< /代码> 还是其他什么?

在这个例子中你会使用什么以及为什么?

运行代码的 CPU 架构如何影响选择?即 PPC 或 x86、32 或 64 位。
运行代码的设备如何影响选择?即桌面、电话、控制台。
该选择与性能和优化有何关系?

简而言之,我的问题是:如何选择使用哪个整数?

stdint.h in C99 provides many options for integer sizes, types and ranges - so many I don't know what ones to choose!

I know how to use size_t and ptrdiff_t when appropriate, and I use fixed size types for storage and transmission. My question concerns values that will only be stored in memory of the host machine.

For example, a structure for an image might contain these members:

struct image {
    integer width, height; /* pixel dimensions of the image */
    integer bits_per_pixel;
    ...
};

If width and height will never exceed SHRT_MAX, should a short be used, or stick with int? An image can't have negative width or height, so use an unsigned type? Perhaps (u)int_least16_t is the correct choice? Something else?

If bits_per_pixel will never exceed a value of 64 use char, unsigned char, uint8_t, int or something else?

What would you use in this example and why?

How does the CPU architecture the code will run on affect the choice? i.e. PPC or x86, 32 or 64bit.
How does the device the code will run on affect the choice? i.e. Desktop, phone, console.
How does the choice relate to performance and optimization?

My question in simple terms is: How do you choose which integer to use?

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

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

发布评论

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

评论(4

丑丑阿 2024-10-04 13:41:30

我想说:不要太担心这个,它通常是一种过早优化的形式。但我的经验法则是:

  • 尽可能使用普通的 int 。它应该是机器的自然字大小。
  • 当您需要明确定义的整数溢出时,请使用无符号类型。
  • 当您需要二进制补码表示时,请使用 (u)intX_t 类型。
  • 对于值为 <= UCHAR_MAX 的大型数组,请使用 unsigned char

请注意, 中的许多类型都是可选的,因此您不能依赖它们的存在。 POSIX 使这稍微好一些。

I would say: Don't worry to much about this, it often is a form of premature optimisation. But my rules of thumb are:

  • Use plain int when possible. It should be the natural word size of the machine.
  • Use unsigned types when you need well-defined integer overflow.
  • Use an (u)intX_t type when you need two's-complement representation.
  • Use unsigned char for large arrays with values <= UCHAR_MAX.

Beware that a lot of the types in <stdint.h> are optional, so you can't depend on their existence. POSIX makes this slightly better.

冷…雨湿花 2024-10-04 13:41:30

对于您的示例,我将简单地对所有三个字段使用 int 或(也许更好)unsigned 。除了包含数千或数百万个元素的数组之外,使用较小的类型是没有意义的;它只是强加了人为的限制。

为了回答更普遍的问题,我遵循以下一些指导原则:

  • 始终为要存储的值选择正确的符号。
  • 对于对象计数、索引、内存中字符串/数据的长度等,请使用 size_t
  • 对于您需要能够存储的具有特定值范围的数据,并且您永远不需要存储超出范围的值,请使用 stdint.h(uint8_tuint16_tuint32_t 等)。此类需求的常见示例包括像素值、音频样本和 Unicode 字符(通常分别为 8 位、16 位和 32 位)。
  • 否则,intunsigned 可能是正确的类型。

For your example, I would simply use int or (perhaps better) unsigned for all three fields. There is no sense using smaller types except in an array that will contain thousands or millions of elements; it just imposes artificial limits.

To answer the more general question, here are some guidelines I go by:

  • Always choose the correct signedness for the values you'll be storing.
  • For object counts, indices, lengths of strings/data in memory, etc. use size_t.
  • For data that has a particular range of values that you need to be able to store, and where you'll never need to store values outside than range, use one of the fixed-size integer types from stdint.h (uint8_t, uint16_t, uint32_t, etc.). The common examples of this sort of need that come to mind are pixel values, audio samples, and Unicode characters (typically 8, 16, and 32 bit, respectively).
  • Otherwise, int or unsigned is probably the right type to use.
淡淡の花香 2024-10-04 13:41:30

没有硬性规定。

如果您选择的类型太小,您最终可能会人为地限制程序可以处理的数据集。太大,您的性能可能会受到影响。

除非您在特定任务中遇到性能问题,否则我肯定会倾向于“太大”。虽然使用整数表示位/像素有点愚蠢,但它可能不会损害更大的方案中的任何内容。

There are no hard and fast rules.

If you choose a type too small, you can end up artificially limiting the data sets that your program can handle. Too big, and your performance may suffer.

Unless you are running in to performance problems for your specific task, I would definitely lean towards "too big". While using an integer for bits/pixel is kind of silly, it probably wont hurt anything in the greater scheme of things.

冬天旳寂寞 2024-10-04 13:41:30

除非您的应用程序确实占用大量内存,否则不要担心大小并使用 int。使用 Short 或 char 可能会导致细微的错误,这些错误可能会在以后引起问题。此外,使用 char 或 Short 不会获得任何额外的 CPU 周期。

Unless your application is really memory intensive, don't worry about sizes and use int. Using short or char can cause subtle bugs which may cause problems later. Also, using char or short won't gain you any additional CPU cycles.

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