Search code examples
c++loopsmethodsintunsigned

Loop - what is the safest variable to use as the index


I would like to know if what's the safest and least memory absorbing methods to make loops in c++. Does loop

for ( unsigned int i = 0;  i < 10; i++){..}

win over loop with just int instead of unsigned int?


Solution

  • The int and unsigned int variables take exactly the same amount of memory and represented in the resulting assembly code in exactly the same way. Thus adding unsigned here changes nothing from the performance point of view.

    In general you should not worry. The loop counter is definitely not the biggest memory consuming thing in your program.