简体   繁体   中英

`short int` vs `int`

Should I bother using short int instead of int ? Is there any useful difference? Any pitfalls?

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.

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 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.

Top-level const for formal arguments is however by convention omitted, possibly because the gain is not enough to outweight the added verbosity.

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.

Cheers & hth.,

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.

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.

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.

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?".

But let's consider some scenarios:

  1. You know the definite range of values that your variable can take.

The ranges for signed integers are:

  • signed char — -2⁷ – 2⁷-1
  • short — -2¹⁵ – 2¹⁵-1
  • int — -2¹⁵ – 2¹⁵-1
  • long — -2³¹ – 2³¹-1
  • long long — -2⁶³ – 2⁶³-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.

It does, however, make sense to use signed char , if you know that you can fit every potential value in the range -128 — 127.

  1. 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.

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).

  1. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM