What to use size_t or int in a for loop?

All use int to count iterations of a loop, and now I read that it makes the program incompatible with x64 architecture when processing large arrays it is better to use size_t. Is that right?
March 19th 20 at 08:33
3 answers
March 19th 20 at 08:35
You have large arrays? Very very?
Don't bother garbage. Int - this is normal.
March 19th 20 at 08:37
Use the solutions in the following priority:
1. ready stl algorithm (e.g. std::find, std::remove_if,...)
2. for range based "for (const auto& elem : myArray)"
3. for (auto iter = my_list.begin()...
4. int
5. size_t

The main problem with int -- irritant of warning about comparing signed and unsigned types in
i < array.size(). A little help std::ssize 20 from C++that returns int. Arrays of size greater than int there are too few to select a default judgment based on them.

Unsigned types lead to a lot of mistakes are associated with negative numbers. Ideally, you should use them only where necessary controlled overflow (operations modulo 2^{32,64}). A simple example where you can shoot yourself in the foot this cycle from 10 to 0.
March 19th 20 at 08:39
Select the type of data that you really need.
If you are in the loop a couple of million iterations of the maximum - then why do you need 64-bit integer?
If you do a little 32-bit number, then you can't use neither int nor size_t for this purpose, you need to use 64-bit integer platform-independent.
And if you have a couple dozen iterations in the jump - you int is too big, get something smaller.

Different tasks need different types of data.
@Lea.Wolff
If you do a little 32-bit number, then you can't use neither int nor size_t for this purpose, you need to use 64-bit integer platform-independent.

Explain why in the mentioned case you can't use size_t? Preferably with links to documentation and standard language. - Vivian.Weissn commented on March 19th 20 at 08:42
@Vivian.Weissnbecause size_t implementation-defined (guaranteed afaik it generally only 16 bit). If you definitely need 64-bit integer, then you not on the way to implementation-defined things, you need a 64-bit integer. - Lea.Wolff commented on March 19th 20 at 08:45
@Lea.Wolff , where are the links to the documentation and the standard? - Vivian.Weissn commented on March 19th 20 at 08:48
@Vivian.Weissn, you wrote "preferably".
Want to say that I was wrong, and even size_t on 32-bit platforms, guaranteed to be 64-bit? - Lea.Wolff commented on March 19th 20 at 08:51
And if you have a couple dozen iterations in the jump - you int is too big, get something smaller
lower int makes no sense to put, because it works the fastest - malika_Will commented on March 19th 20 at 08:54
@Lea.Wolff, If we are talking about iterations memory (data array) on 32 bit platforms, why not have a 64 bit counter, because the memory physically is not enough for addressing the second half of the indices in the 64 bit counter.
Therefore, in x32 platforms size_t 32 bit and x64 64-bit, i.e., the selected optimal size of the counter for each platform. size_t can address the maximum amount of data for a particular platform, this is stated in the standard.
Also, use a signed type to index array elements - that's a good idea - the index is usually positive, so using int, we lose 1 bit in the counter (which is half of all possible values of the counter).

Counter in a loop is not necessarily used as an index into the array, so it is always necessary to look at the situation. Sometimes it is really necessary to use a uint64_t under the counter, and sometimes uint8_t enough. - nickolas.Casper commented on March 19th 20 at 08:57
@nickolas.Casper,
Counter in a loop is not necessarily used as an index into the array, so it is always necessary to look at the situation.

So I'm about the same write.
From the text of the question should not (in my opinion) that the question relates only to operate the array in memory. A question about the iteration counter. - Lea.Wolff commented on March 19th 20 at 09:00
@Lea.Wolff , the type size_t should be used only when the value reflects quantitative or positional characteristic of the memory block. For an arbitrary discrete iterations, you should use any size integer type, but not size_t, because only in this case it is not suitable because of their ambiguity.

In documentation specified that the type size_t can store the maximum size of a theoretically possible object, including the size of the array. It is assumed that the object size can be contained in the value of the type size_tis ill-formed.

On x86 there is no need to store 64-bit type size_t, because all questions of the size and position of the memory block to fit the size of the General purpose register is 4 bytes. The size of type size_t on x86 is equal to 32 bits. On x64 architecture type size_t will have to take the required 8 bytes. Mobile architectures are all exactly the same. About it tells the status of the Implementation-definedin standard: size quantitative or positional characteristics of the memory block is determined by the details of the target architecture, and always large enough to safely store its value.

Well, even at the expense of this.
And if you have a couple dozen iterations in the jump - you int is too big, get something smaller.

There is such a thing as a ranking integer types. You're using the smaller type and I think that it is used, but will use the type with big size, consistent with the size of the General purpose register.
And everything would be cool, but this optimization is in the wrong hands leads directly to Undefined Behavior (look for the words: "we forget about the expansion of the integer types").
Ie you fully understand what you suggest man. - Vivian.Weissn commented on March 19th 20 at 09:03

Find more questions by tags C++