13

Consider that int take 4 bytes in memory.

to understand what I'm looking for take this example :

for(x=0;x<10;x++) //do something

in this for instruction I know that the value of x is less than 11,

I have seen lot of code and most people declare x like an int,

why we shouldn't or why most people doesn't declare x like a short or even like a char !!

I thought in the reason and I found this explanation, for example :

short s=5;

s take 2 bytes in memory, and what I know is that the compiler consider 5 like an int so to put 5 to s, 5 should be converted to short right !!

-> so this instruction take less memory but more work

int i=5;

here i take 4 bytes but with no need for conversation (5 is an int)

-> so this instruction do less work but take more memory

is the reason something like what I thought !!

I hope that my question was clear

Az.Youness
  • 2,167
  • 1
  • 24
  • 33
  • 2
    `in this for instruction I know that the value of x is less than 11`, even though you're right, `x` is less than **10**. – zmo Jun 18 '13 at 15:55
  • 11
    Leave such micro-optimizations to optimizing compilers (e.g. `gcc -O2`). They do better than what you can do. Focus on correctness & readability of your code. – Basile Starynkevitch Jun 18 '13 at 15:56
  • 9
    int is typically the register width so assigning to char or short may actually involve more work than assigning to int. – Pete Jun 18 '13 at 15:56
  • @BasileStarynkevitch +1 for readability :) – VoidPointer Jun 18 '13 at 15:57
  • Chances are, `int` will be as efficient timewise as anything shorter, and if the variable is kept in a register by the compiler, then neither `short` nor `char` provides any benefit compared with `int`. – Jonathan Leffler Jun 18 '13 at 15:57
  • 3
    @zmo While evaluating the final (failing) condition, `x` is indeed 10 and needs to be large enough to hold it. – Kevin Jun 18 '13 at 15:58
  • @zmo: Not during the final condition evaluation, it isn't. – Lightness Races in Orbit Jun 18 '13 at 16:05
  • @LightnessRacesinOrbit afaict, during the final condition evaluation it is equal to 10, where `x<10` will return false and then end the loop evaluation. – zmo Jun 18 '13 at 16:08
  • 2
    @zmo : the point Kevin and LightnessRacesinOrbit are making is that 10 is not less than 10. 10 is less than 11 though. – Sander De Dycker Jun 18 '13 at 16:10
  • @SanderDeDycker well, iinw, in written/spoken languages "less" is equivalent to "less or equal", the same way that "or" is equivalent to an "xor". And in this context he would make his point telling that `10` is actually way less than `127` or `65535` depending on the chosen size type... – zmo Jun 18 '13 at 16:16
  • 1
    @zmo: "Less" is absolutely _not_ equivalent to "less or equal", neither in English neither in C++. I suggest you check all your source code and other writing for off-by-one errors, immediately! – Lightness Races in Orbit Jun 18 '13 at 16:19
  • don't get me wrong, I'm not saying it does in C++, I'm just talking about spoken languages. And I'm not a native english speaker. But anyway as the question is closed, it's either time to end that troll, or to fork on english.stackoverflow :-p I prefer to code that to troll, so I'd for the first option! – zmo Jun 18 '13 at 16:23
  • 1
    @zmo: I _am_ a native English speaker, and that is not the case ;) – Lightness Races in Orbit Jun 18 '13 at 16:23
  • 1
    This is an excellent question; I don't understand why it has been closed. – alecov Jun 18 '13 at 16:56
  • The title has been changed !! This title Does not reflect correctly what I'm looking for !! – Az.Youness Jun 19 '13 at 10:49

7 Answers7

17

If you have to store millions of numbers in memory and each number could be between 0 and 11, then you'd be concerned with memory. In a loop, the variable is most likely stored in a CPU register which means it is, for example, 32-bit on x86, or 32 to 64 bits on x86_64, etc. All “smaller” integers would be zero-extended to 32 or 64 bit anyway.

int is simple and readable, so lots of people use it. But if you must worry about performance or hint the compiler about size constraints then use "(u)int_fast_*" types (i.e. uint_fast8_t.

9

You are thinking too much about the surface look of things. Reality differs from that.

For example, you are worried about the memory a loop variable takes. However, in many loops the loop variable won't ever be stored in memory. Instead it will be held in a register. The number of registers in the CPU is limited, but variables cannot take up half a register (usually - x86 is a bit strange there), so whether you use int, short or char, you probably lose a full register anyway. So you don't save anything by making the variable smaller.

Similar is your assumption that assigning an integer literal to a short takes more work than to an int. Here the problem is the assumption that the compiler would generate code that does some kind of conversion at runtime, when it is far simpler to just generate code that does the simple thing (just store the literal to the memory location) in the first place.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
8

The best reason of all - readability. If I saw a loop iterating over a short or a char, I'd spend a bit of time to figure out why. An int is more intuitive because it's the most used type for iteration (even more used that iterator or size_t).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

It sometimes makes sense to micro-optimize memory usage by choosing arithmetic types smaller than int. But that comes at a cost, because, formally, the value is promoted to int to do arithmetic on it, then converted back to the smaller type. int is the natural size for the target platform, so it's almost always better to just use it, especially since every future maintainer will have to figure out why someone wrote such unnatural code.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
4

"Plain ints have the natural size suggested by the architecture of the execution environment"1, which means that in a typical case, a plain int is the type that imposes the least work on the processor to manipulate and work with. In short, int is the default type you normally want to use when you don't have a fairly good reason to use something else.

Also note that the reduction in memory usage from using a short or a char in the cited case may well be entirely illusory. In a typical case, we can expect a loop index variable to be allocated in a register anyway, so regardless of how few bits we care about, the variable occupies essentially the entire register in any case. If it's not in a register, it'll typically be on the stack, and on most cases the sizes of items on the stack are also fixed (32 bits on a 32-bit architecture, 64 bits on a 64-bit architecture, etc.) so allocating a single char (for example) can/will frequently end up using just as much memory as an int would have anyway.


  1. §3.9.1/2 in n1337, but all the standards for C and C++ going all the way back to the original ANSI C89 standard have had virtually identical phrasing, though the section numbers have changed.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    This is actually the _only_ answer which correctly answers the question objectively, and doesn't play the smart ass on the questioner ("Leave micro optimizations alone", "Do _this_ instead of _that_", etc.) I wish there were more answers like this in SO. – alecov Jun 18 '13 at 17:03
  • Unfortunately, `int` has to be at least 16 bits wide, which means that this doesn't hold on 8-bit architectures. – dyp Jun 18 '13 at 17:05
1

No, not completely. If you have a semi reasonable compiler, it will deal with this correctly. Use a short if you're that concerned about memory usage, and leave it at that.

int is probably better though, as it is usually the size of the register. This can be more efficient on some CPU architectures.

See this question too.

Community
  • 1
  • 1
Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

You may come across code when you need to loop for larger no. of times than what can a char or even a short may hold. This may be the situation when you are coding for say Database like systems where the looping is though over huge numbers. To be on the safer side its good to take a loop variable as 'int'. One more thing, if you are using such a loop for something like a Delay_ms() function, and if you try to restrict the loop variable to char, you will have to have nested loops, or call the same Delay_ms() function again and again.