C++ 什么时候在 `int` 上使用 `short`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24371077/
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-08-28 00:42:54  来源:igfitidea点击:

When to use `short` over `int`?

c++intshort

提问by dayuloli

There are many questions that asks for difference between the shortand intinteger types in C++, but practically, when do you choose shortover int?

有很多问题,对于差的要求shortint整数C ++类型,但实际上,当你选择shortint

回答by dayuloli

(See Eric's answerfor more detailed explanation)

(有关更详细的解释,请参阅Eric 的回答

Notes:

笔记:

  • Generally, intis set to the 'natural size' - the integer form that the hardware handles most efficiently
  • When using shortin an array or in arithmetic operations, the shortinteger is converted into int, and so this can introduce a hit on the speed in processing shortintegers
  • Using shortcan conserve memory if it is narrower than int, which can be important when using a large array
  • Your program will use more memory in a 32-bit intsystem compared to a 16-bit intsystem
  • 通常,int设置为“自然大小” - 硬件处理最有效的整数形式
  • 当使用short以阵列或算术运算中,short整数被转换成int,所以这可以在处理上的速度引入命中short整数
  • short如果比 窄int,使用可以节省内存,这在使用大数组时很重要
  • 你的程序将在一个32位的使用更多的内存int系统相比,16位int系统

Conclusion:

结论:

  • Use intunless you conserving memory is critical, or your program uses a lot of memory (e.g. many arrays). In that case, use short.
  • 使用int除非你节省内存是至关重要的,或者你的程序使用了大量的存储器(例如许多阵列)。在这种情况下,请使用short.

回答by Eric Finn

You choose shortover intwhen:

你选择shortint当:

Either

任何一个

  • You want to decrease the memory footprint of the values you're storing (for instance, if you're targeting a low-memory platform),
  • You want to increase performance by increasing either the number of values that can be packed into a single memory page (reducing page faults when accessing your values) and/or in the memory caches (reducing cache misses when accessing values), and profiling has revealed that there are performance gains to be had here,
  • Or you are sending data over a network or storing it to disk, and want to decrease your footprint (to take up less disk space or network bandwidth). Although for these cases, you should prefer types which specify exactly the size in bits rather than intor short, which can vary based on platform (as you want a platform with a 32-bit shortto be able to read a file written on a platform with a 16-bit short). Good candidates are the types defined in stdint.h.
  • 您想减少所存储值的内存占用(例如,如果您的目标是低内存平台),
  • 您希望通过增加可以打包到单个内存页面(访问值时减少页面错误)和/或内存缓存(访问值时减少缓存未命中)的值数量来提高性能,并且分析显示这里有性能提升,
  • 或者您正在通过网络发送数据或将其存储到磁盘,并希望减少占用空间(以占用更少的磁盘空间或网络带宽)。尽管对于这些情况,您应该更喜欢以位而不是int或精确指定大小的类型short,这可能因平台而异(因为您希望具有 32 位的平台short能够读取在具有16 位short)。好的候选者是stdint.h 中定义的类型。

And:

和:

  • You have a numeric value which does not need to take on any values that can't be stored in a shorton your target platform (for a 16-bit short, this is -32768-32767, or 0-65535for a 16-bit unsigned short).
  • Your target platform (or one of you r target platforms) uses less memory for a shortthan for an int. The standard only guarantees that shortis not largerthan int, so implementations are allowed to have the same size for a shortand for an int.
  • 您有一个数值,它不需要采用无法存储在short目标平台上的任何值(对于 16 位short,这是-32768- 32767,或0-65535对于 16 位unsigned short)。
  • 您的目标平台(或您的目标平台之一)使用的内存shortint. 唯一标准来保证short不大于int,因此允许实现具有相同的尺寸要short和用于int

Note:

笔记:

chars can also be used as arithmetic types. An answer to "When should I use charinstead of shortor int?"would read very similarly to this one, but with different numbers (-128-127for an 8-bit char, 0-255for an 8-bit unsigned char)

chars 也可以用作算术类型。回答“我什么时候应该使用char而不是shortint?” 读起来与这个非常相似,但有不同的数字(-128-127表示 8 位char0-255表示 8 位unsigned char

In reality, you likely don't actually want to use the shorttype specifically. If you want an integer of specific size, there are types defined in <cstdint>that should be preferred, as, for example, an int16_twill be 16 bits on every system, whereas you cannot guarantee the size of a shortwill be the same across all targets your code will be compiled for.

实际上,您可能实际上并不想short专门使用该类型。如果您想要一个特定大小的整数,则应该首选定义<cstdint>其中的类型,例如,int16_t每个系统上的an将是 16 位,而您不能保证 a 的大小short在您的代码的所有目标中都相同将被编译。

回答by Thomas Matthews

In general, you don't prefer shortover int.

一般来说,你不喜欢shortover int

The int type is the processor's native word size
Usually, an intis the processor's word size.

int 类型是处理器的本机字长。
通常,anint是处理器的字长。

For example, with a 32-bit word size processor, an intwould be 32 bits. The processor is most efficient using 32-bits. Assuming that shortis 16-bit, the processor still fetches 32-bits from memory. So no efficiency here; actually it's longer because the processor may have to shift the bits to be placed in the correct position in a 32-bit word.

例如,对于 32 位字长的处理器,anint将为 32 位。处理器使用 32 位时效率最高。假设它short是 16 位,处理器仍然从内存中获取 32 位。所以这里没有效率;实际上它更长,因为处理器可能必须移动位以放置在 32 位字中的正确位置。

Choosing a smaller data typeThere are standardized data types that are bit specific in length, such as uint16_t. These are preferred to the ambiguous types of char, short,and int. These width specific data types are usually used for accessing hardware, or compressing space (such as message protocols).

选择较小的数据类型有一些长度特定于位的标准化数据类型,例如uint16_t. 这些是优选的暧昧类型char, short,int。这些特定于宽度的数据类型通常用于访问硬件或压缩空间(例如消息协议)。

Choosing a smaller range
The shortdata type is based on rangenot bit width. On a 32-bit system, both shortand intmay have the same 32-bit length.

选择一个较小的范围内
short数据类型是基于范围没有被比特宽度。在 32 位系统上,shortint可能具有相同的 32 位长度。

Once reason for using shortis because the value will never go past a given range. This is usually a fallacy because programs will change and the data type could overflow.

使用一次的原因short是因为该值永远不会超过给定的范围。这通常是一种谬论,因为程序会改变并且数据类型可能会溢出。

Summary
Presently, I do not use shortanymore. I use uint16_twhen I access 16-bit hardware devices. I use unsigned intfor quantities, including loop indices. I use uint8_t, uint16_tand uint32_twhen size matters for data storage. The shortdata type is ambiguous for data storage, since it is a minimum. With the advent of stdintheader files, there is no longer any need for short.

总结
目前,我不再使用short了。我uint16_t在访问 16 位硬件设备时使用。我unsigned int用于数量,包括循环索引。我使用uint8_tuint16_tuint32_t在大小事务用于数据存储。的short数据类型是不明确的用于数据存储,因为它是一个最小值。随着stdint头文件的出现,不再需要short.

回答by Zaphod Beeblebrox

If you don't have any specific constraints imposed by your architecture, I would say you can always use int. The type shortis meant for specific systems where memory is a precious resource.

如果您的架构没有任何特定限制,我会说您始终可以使用int. 该类型short适用于内存是宝贵资源的特定系统。