Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast from size_t to int, or iterate with size_t?

Tags:

c++

Is it better to cast the iterator condition right operand from size_t to int, or iterate potentially past the maximum value of int? Is the answer implementation specific?

int a;
for (size_t i = 0; i < vect.size(); i++)
{
    if (some_func((int)i))
    {
        a = (int)i;
    }
}

int a;
for (int i = 0; i < (int)vect.size(); i++)
{
    if (some_func(i))
    {
        a = i;
    }
}
like image 883
Nick Bolton Avatar asked Aug 12 '09 18:08

Nick Bolton


3 Answers

I almost always use the first variation, because I find that about 80% of the time, I discover that some_func should probably also take a size_t.

If in fact some_func takes a signed int, you need to be aware of what happens when vect gets bigger than INT_MAX. If the solution isn't obvious in your situation (it usually isn't), you can at least replace some_func((int)i) with some_func(numeric_cast<int>(i)) (see Boost.org for one implementation of numeric_cast). This has the virtue of throwing an exception when vect grows bigger than you've planned on, rather than silently wrapping around to negative values.

like image 124
Mike Elkins Avatar answered Sep 19 '22 13:09

Mike Elkins


I'd just leave it as a size_t, since there's not a good reason not to do so. What do you mean by "or iterate potentially up to the maximum value of type_t"? You're only iterating up to the value of vect.size().

like image 36
mipadi Avatar answered Sep 21 '22 13:09

mipadi


For most compilers, it won't make any difference. On 32 bit systems, it's obvious, but even on 64 bit systems, both variables will probably be stored in a 64-bit register and pushed on the stack as a 64-bit value.

If the compiler stores int values as 32 bit values on the stack, the first function should be more efficient in terms of CPU-cycles.

But the difference is negligible (although the second function "looks" cleaner)

like image 23
Philippe Leybaert Avatar answered Sep 20 '22 13:09

Philippe Leybaert