Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have an infinite loop? Simple question, short code

If the input array is empty, then array.size() should be 0. The first for, that goes from 0 to array.size() - 1, should mean it goes from 0 to -1, right?

This for then, should not be entered and the function should return the inversionsCounter value which would be 0

But this doesn't happen, and the code enters an infinite loop. Why is this?

Here is the code:

#include <vector>
#include <iostream>

using namespace std;

int countInversions(vector<int> array)
{    
    int inversionsCounter = 0;
    for (int i = 0; i < array.size() - 1; ++i)
        for (int j = i + 1; j < array.size(); ++j)
            if (array[i] > array[j])
                ++inversionsCounter;

    return inversionsCounter;
}

int main()
{
    vector<int> array = {};
    cout << array.size();
    cout << countInversions(array);
}
like image 200
maverick.01 Avatar asked Apr 13 '26 11:04

maverick.01


1 Answers

The type of the return value of the member function size of the class template std::vector is unsigned integer type usually equivalent to the type size_t.

So in the condition of the for loop

for (int i = 0; i < array.size() - 1; ++i)

due to the usual arithmetic conversions the both operands of the expression

i < array.size() - 1

are converted to the unsigned integer type that corresponds to the type std::vector<int>::size_type. As a result the expression array.size() - 1 is converted to a maximum value of the type when the member function size returns 0.

Here is a demonstrative program.

#include <iostream>
#include <iomanip>

int main() 
{
    std::cout << size_t( -1 ) << '\n';
    std::cout << std::boolalpha << ( 0 < size_t( -1 ) ) << '\n';    
    
    return 0;
}

Its output is

18446744073709551615
true

So when the member function size returns 0 you have in fact a loop like this

for (int i = 0; i < 18446744073709551615; ++i)

Instead of the type int you should use at least the type size_t for indices. And the outer loop should look like

for ( size_t i = 0; i < array.size(); ++i )
like image 158
Vlad from Moscow Avatar answered Apr 15 '26 01:04

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!