типы-данных – What type of variable should be used in the loop counter and array declaration?


I'll try to explain.

In any programming language, everyone is always fighting to ensure that the program eats as little memory as possible. As if there was a leak. Especially in C++, you need to correctly allocate, release.

Each data type occupies a certain amount of memory space in bytes.

Examples will be written in C#

Explain to me please. Why do programmers, knowing, for example, that in a loop it will run through an array of 10 characters long and this length will be constant, they still write

for (int i = 0; i < someVar.Length; i++) { 
// do something

Why is the counter type int ? Why not sbyte or byte ? Yes, maybe it will work quickly in this scope. But there are many such blocks in the code.

Likewise with arrays. Knowing exactly the length ( Correction. Because there are already answers, but the question was asked … a little confusing …: not the length, of course, but knowing the maximum numbers for sure .. for example, what will be the numbers from 1 to 100 ). All the same, often everything is announced

int[] arr = new int[8];

instead of

ushort[] arr = new ushort[8];


byte[] arr = new byte[8];

This array will occupy memory until it is killed or the program terminates.

So hence the question. Why is everyone doing this? Is there something I don't know yet or is it just programmer's laziness?


Your basic assumption – saving memory at all costs – is wrong.

For example, the processor works much more efficiently with the int data type than with the byte data type, so programmers and the compiler use int wherever possible to speed up work, neglecting small losses in memory and gaining performance. Similarly, the data type double is usually used instead of float because it is faster, although it takes up twice as much space.

Then, data structure alignment. Compilers insert extra bytes between the fields of data structures for alignment and thus faster access.

Another example is loop unrolling and function inlining. The optimizing compiler unrolls the loop

for (int i = 0; i < 10; i++) f(i);


f(0); f(1); f(2); f(3); f(4); f(5); f(6); f(7); f(8); f(9);

because it's faster. No one needs a gain of three bytes, a gain of three milliseconds is felt by anyone.

Next, about the cycle. In modern programming, the code should be general enough not to emphasize low-level optimizations, but to focus on the semantics, the meaning of the code. Therefore, they try to hide low-level details wherever possible.

From this point of view, the detail that the given array has specifically 10 elements, and 4 bits could be used to index it (savings!), Is simply ignored. Moreover. the possibility of indexing by a “narrow” data type is not checked by the compiler, which means that if tomorrow this code receives an array with more elements as input, the code will silently stop working.

The correct modern approach to iterate over the elements of an array is:

foreach (var item in array)
    // process item

Here we abstract from the size of the array (it will work with any size), from the specific iteration method (we do not explicitly encode the order of enumeration of elements), from the array itself (the same code will work with the list), thus shifting the care of low-level optimizations to the compiler , and make the code easier to maintain.

Don't make simple, trivial things difficult. Save the power of your thought process for really complex problems and for algorithmic optimization. Let the compiler optimize for you, believe me, he can do it better.

Scroll to Top