1

I read here Benefit of using short instead of int in for... loop that it doesn't really make sense to use short instead of plain int in loop counters because in most architectures short will occupy the same amount of memory space as int. That makes me think that short ints are really not useful because they don't result in what one might expect, that is saving memory. However, in production code I often see that short ints are used as function arguments. For example, when I was browsing coreboot (coreboot.org) using ack-grep I found many functions that take short. I realize that coreboot code is rather low-level but I wonder what is the benefit, if any, of using short instead of int in C on x86 architecture? When should I use it?

Community
  • 1
  • 1
user1042840
  • 1,925
  • 2
  • 16
  • 32
  • 3
    The biggest benefit to using `short` comes when they're used in aggregate -- in an array of `short` IOW -- as that array takes up half the space of an array of `int`. The other possible benefit is that if the function is defined to take a `short`, you know that the value will be in the range -32768..+32767 (on systems with 2's complement integers and 16-bit `short`, which is most systems), which may simplify your validation processes inside the function. Otherwise, variables of type `short` aren't of a lot of help unless you're matching an external specification of some sort. – Jonathan Leffler Sep 30 '13 at 21:14
  • @JonathanLeffler why not make this an answer? – ouah Sep 30 '13 at 21:17
  • By `validation process' - do you mean checking for overflow? How would short make that easier? – user1042840 Sep 30 '13 at 21:17

2 Answers2

2

The biggest benefit to using short comes when they're used in aggregate — primarily in an array of short IOW, or possibly as members of a structure ‐ as an array of short normally takes up half the space of an array of int with the same number of entries.

The other possible benefit is that if the function is defined to take a short, you know that the value will be in the range -32768..+32767 (on systems with 2's complement integers and 16-bit short, which is most systems), which may simplify your validation processes inside the function. If the type passed were int instead of short but the range of valid values is -32788..+32767, then you would have to validate that the passed value was in range (assuming sizeof(int) > sizeof(short), which is not guaranteed but is typical).

Otherwise, variables of type short aren't a lot of help unless you're matching an external specification of some sort.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Some functions are meant for short and others for int.

If one wanted to count all the bits set in a short one would use

int CountBits_short(short x);

If one wanted to count all the bits set in a int one would use

int CountBits_int(int x);

There is not performance issue here, just a functional difference.


@JonathanLeffler comment to the OP sums up the most useful reason for short in they may take up less space.


Other

Other thoughts:

On platforms where sizeof(int) == sizeof(short) there is no performance benefit.
Otherwise we know sizeof(int) > sizeof(short).

The compiler may make similar entry/exit code for the following and no performance difference.

void foo_s(short bar);
void foo_i(int bar);

In passing pointer as in

void foo_s(short *bar);
void foo_i(int *bar);

The sizeof(bar) is the same, for it is an address, again no benefit in augment passing.

In passing arrays as in

void foo_s(short bar[]);
void foo_i(int bar[]);

bar becomes the address of the first element, again the same size, for it is an address, again no benefit in augment passing.

Performance of functions that use short are nominally thought to be slightly worse than int in that various amounts of code may be needed to promote / demote between short and the processor's preferred int. int by definition *1 is the processor's nominally preferred integer size.

Of course any performance issues need profiling to validate, but the gist is that short is used to save space at a potential nominal cost in performance.

*1 still looking for spec def, so for now this is my opinion.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256