Gingerbread
Gingerbread

Reputation: 53

Infinite loop occurs when indexing an array

int main()
{
    int i;
    int vals[5];

    for (i = 0; i <= 5; i++)
    {
        vals[i] = 0;
    }

    printf("%d\n", i);
    return 0;
} 

I'm trying to learn C from a university tutorial. They mentioned that in the above code:

The for loop is reset when i reaches 5, and the program goes through the loop again, and again, repeatedly.

I couldn't understand why and I tried to figure it out in the pythontutor.com, it prints 6 (It's experimental now for C.) What's the proper output and why this occurs?

Upvotes: 1

Views: 1655

Answers (3)

abhiarora
abhiarora

Reputation: 10440

Variables having automatic storage class (variables created in a function except static variables) are created in the stack in C programming language.

The variables in your program are also created in stack. Therefore, the variables i, vals[0], vals[1], vals[2], vals[3] and vals[4] can be found in stack.

Remember: Array indices in C programming language starts from 0. If the size of array is n, then indices go from 0 to (n-1).

The for loop in your program is trying to access vals[5] when your index variable reaches 5. Reaching outside the bounds of array is an UNDEFINED BEHAVIOR in C. The result you have been seeing is one of the possible outcome. Since, vals[5] doesn't exist in your stack, you could be accessing memory location assigned to some other variable which could be memory location assigned to variable i. You can even get Segmentation Fault if you access an array out of it's bound (As I have mentioned above, the result is Undefined. You program may run without any segmentation fault or you may get it).

Consider the picture that I have created:

enter image description here

In your case, the array vals and the index i may be next to each other in stack. So, vals[5] = 0 resets i to zero and hence infinite loop.

Upvotes: 0

Array indices in C are 0 based. So if your array is of size 5, the indices go from 0 to 4.

You loop for (i = 0; i <= 5; i++) reaches into index 5, which is outside the bounds of the array.

Reaching outside the bounds of a buffer is undefined behavior in C. Anything is allowed to happen. And your program glitching out completely, is one possible outcome.

It's probably happening because the array and the index are next to each other in memory. So vals[5] = 0 sets i to zero. And henceforth the loop condition is forever satisfied.

Upvotes: 6

Gerhardh
Gerhardh

Reputation: 12404

This code invokes undefined bahavior. Which of course means that what they stated, could actually happen. But this largely depends on many details.

When they state that i will be reset, they assume a certain location of the variables on the stack. As soon as you execute

vals[i] = 0;

with i==5 you could actually write to the location where i is stored. But on the other hand, i could just be hold in a register or somewhere else on the stack and you wouldn't get infinite loop.

Upvotes: 5

Related Questions