C++ `short int` 与 `int`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4445303/
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
`short int` vs `int`
提问by Cheers and hth. - Alf
Should I bother using short int
instead of int
? Is there any useful difference? Any pitfalls?
我应该费心使用short int
而不是int
吗?有什么有用的区别吗?有什么陷阱吗?
采纳答案by EboMike
What Ben said. You will actually create less efficient code since all the registers need to strip out the upper bits whenever any comparisons are done. Unless you need to save memory because you have tons of them, use the native integer size. That's what int
is for.
本说的话。您实际上会创建效率较低的代码,因为每当进行任何比较时,所有寄存器都需要去除高位。除非您因为有大量内存而需要节省内存,否则请使用本机整数大小。这int
就是为了。
EDIT: Didn't even see your sub-question about const. Using const on intrinsic types (int, float) is useless, but any pointers/references should absolutely be const whenever applicable. Same for class methods as well.
编辑:甚至没有看到你关于 const 的子问题。在内部类型(int、float)上使用 const 是没有用的,但任何指针/引用都应该在适用时绝对是 const。类方法也是如此。
回答by Cheers and hth. - Alf
short vs int
短与整数
Don't bother with short
unless there is a really good reason such as saving memory on a gazillion values, or conforming to a particular memory layout required by other code.
不要打扰,short
除非有很好的理由,例如将内存保存在无数个值上,或者符合其他代码所需的特定内存布局。
Using lots of different integer types just introduces complexity and possible wrap-around bugs.
使用许多不同的整数类型只会引入复杂性和可能的环绕错误。
On modern computers it might also introduce needless inefficiency.
在现代计算机上,它也可能导致不必要的低效率。
const
常量
Sprinkle const
liberally wherever you can.
const
随意洒在任何地方。
const
constrains what might change, making it easier to understand the code: you know that this beastie is not gonna move, so, can be ignored, and thinking directed at more useful/relevant things.
const
限制可能改变的内容,使代码更容易理解:您知道这个野兽不会移动,因此可以忽略,并针对更有用/相关的事物进行思考。
Top-level const
for formal arguments is however by convention omitted, possibly because the gain is not enough to outweight the added verbosity.
const
然而,按照惯例,形式参数的顶级被省略,可能是因为增益不足以超过增加的冗长。
Also, in a pure declaration of a function top-level const
for an argument is simply ignored by the compiler. But on the other hand, some other tools may not be smart enough to ignore them, when comparing pure declarations to definitions, and one person cited that in an earlier debate on the issue in the comp.lang.c++ Usenet group. So it depends to some extent on the toolchain, but happily I've never used tools that place any significance on those const
s.
此外,在函数顶层的纯声明中const
,编译器会简单地忽略参数。但另一方面,在将纯声明与定义进行比较时,一些其他工具可能不够聪明而无法忽略它们,并且有人在早先在 comp.lang.c++ Usenet 组中就该问题进行的辩论中引用了这一点。所以它在某种程度上取决于工具链,但很高兴我从未使用过对这些const
s 有任何意义的工具。
Cheers & hth.,
干杯 & hth.,
回答by Ben Hymanson
Absolutely not in function arguments. Few calling conventions are going to make any distinction between short and int. If you're making giant arrays you could use short
if your data fits in short
to save memory and increase cache effectiveness.
绝对不在函数参数中。很少有调用约定会在 short 和 int 之间做出任何区分。如果您正在制作巨型阵列,您可以short
在数据适合时使用它short
来节省内存并提高缓存效率。
回答by v010dya
The question is technically malformed "Should I use short int
?". The only good answer will be "I don't know, what are you trying to accomplish?".
问题在技术上是格式错误的“我应该使用short int
吗?”。唯一好的答案是“我不知道,你想完成什么?”。
But let's consider some scenarios:
但让我们考虑一些场景:
- You knowthe definite range of values that your variable can take.
- 您知道您的变量可以取值的确定范围。
The ranges for signed integers are:
有符号整数的范围是:
signed char
— -2? – 2?-1short
— -21? – 21?-1int
— -21? – 21?-1long
— -231 – 231-1long long
— -2?3 – 2?3-1
signed char
— -2?– 2?-1short
— -21?– 21?-1int
— -21?– 21?-1long
— -231 — 231-1long long
— -2?3 – 2?3-1
We should note here that these are guaranteed ranges, they can be larger in your particular implementation, and often are. You are also guaranteed that the previous range cannot be larger than the next, but they can be equal.
我们在这里应该注意,这些是有保证的范围,在您的特定实现中它们可以更大,并且通常是。您还可以保证前一个范围不能大于下一个范围,但它们可以相等。
You will quickly note that short
and int
actually have the same guaranteed range. This gives you very little incentive to use it. The only reason to use short
given this situation becomes giving other coders a hint that the values will be not too large, but this can be done via a comment.
您很快就会注意到,short
并且int
实际上具有相同的保证范围。这使您几乎没有动力使用它。在short
这种情况下使用的唯一原因是向其他编码人员提示值不会太大,但这可以通过注释来完成。
It does, however, make sense to use signed char
, if you know that you can fit every potential valuein the range -128 — 127.
但是,signed char
如果您知道可以拟合-128 - 127 范围内的每个潜在值,那么使用 确实是有意义的。
- You don't know the exact range of potential values.
- 您不知道潜在值的确切范围。
In this case you are in a rather bad position to attempt to minimise memory useage, and should probably use at least int
. Although it has the same minimum range as short
, on many platforms it may be larger, and this will help you out.
在这种情况下,您在尝试最小化内存使用方面处于相当不利的位置,并且应该至少使用int
. 尽管它与 具有相同的最小范围short
,但在许多平台上它可能更大,这将对您有所帮助。
But the bigger problem is that you are trying to write a piece of software that operates on values, the range of which you do not know. Perhaps something wrong has happened before you have started coding (when requirements were being written up).
但更大的问题是您正在尝试编写一个对值进行操作的软件,而您不知道其范围。也许在您开始编码之前(在编写需求时)发生了错误。
- You have an idea about the range, but realise that it can change in the future.
- 您对范围有一个想法,但意识到它可能会在未来发生变化。
Ask yourself how close to the boundary are you. If we are talking about something that goes from -1000 to +1000 and can potentially change to -1500 – 1500, then by all means use short
. The specific architecture may pad your value, which will mean you won't save any space, but you won't lose anything. However, if we are dealing with some quantity that is currently -14000 – 14000, and can grow unpredictably (perhaps it's some financial value), then don't just switch to int
, go to long
right away. You will lose some memory, but will save yourself a lot of headache catching these roll-over bugs.
问问自己离边界有多近。如果我们谈论的是从 -1000 到 +1000 并且可能会更改为 -1500 – 1500 的东西,那么一定要使用short
. 特定的架构可能会增加您的价值,这意味着您不会节省任何空间,但不会丢失任何东西。但是,如果我们正在处理当前为 -14000 – 14000 的某个数量,并且可能会不可预测地增长(可能是某种财务价值),那么不要只是切换到int
,而是立即转到long
。您会丢失一些内存,但会为您解决这些翻转错误而省去很多麻烦。
回答by Sanjit Saluja
short vs int- If your data will fit in a short, use a short. Save memory. Make it easier for the reader to know how much data your variable may fit.
short vs int- 如果您的数据适合short,请使用short。节省内存。让读者更容易知道您的变量可能适合多少数据。
use of const- Great programming practice. If your data should be a const then make it const. It is very helpful when someone reads your code.
使用 const- 很棒的编程实践。如果您的数据应该是常量,则将其设为常量。当有人阅读您的代码时,这非常有帮助。