0

I found a very strange c++ code. Can someone explain the for-loop condition for me?
How come it just has an array and index variable?

include <iostream>

using namespace std;

int main()
{
   int a[] = {1, 2, 3, 4, 5};

   for (int i = 0; a[i];i++){
      cout << a[i] << endl;
   }

  return 0;
}

The result is:

1

2

3

4

5

-858993460

2424376

12655176

1

3492888

3483368

1402216725

Why does it output 12 elements? Where are those additional 7 elements from? Many Thanks!

1
  • 3
    Because of undefined behavior. Commented Jun 17, 2013 at 19:47

6 Answers 6

7

In C/C++ it is OK to put a number in place of a boolean condition. When this happens, the result is considered false when the number is zero; otherwise, it is considered true.

Now you can easily see that the loop has an invalid termination condition: it expects to stop when the first zero is found in the array, but the array has no zeros, and so the loop continues beyond the array bounds. In your case, it finds a zero after seven additional iterations, producing junk values (it could have crashed instead, because it's undefined behavior).

Changing the array initializer to include zero would fix the problem:

int a[] = {1, 2, 3, 4, 5, 0};

Of course, iterating up to the number of elements in the array would have worked as well.

Sign up to request clarification or add additional context in comments.

Comments

4

The expression

a[i]

is is equivalent to

a[i] != 0

in a context that expects a condition. So the loop iterates until it finds an element in the array that is zero. Since no element within the bonds of the array satisfies this condition, the code reads the array out of bounds, so your program invokes undefined behavior (and prints the values at the requested memory locations, which appear to be garbage).

Comments

3

The code has undefined behavior, so anything could happen. The array has 5 elements so the last index allowed to dereference is 4. As soon as you use value of arr[5] your program is hopelessly derailed.

(In this particular run it probably found a trash looking as 0 at the 13th slot so stopped there, but as mentioned it would be okay if you seen no output at all or have mailed your boss).

Comments

1

Its an undefined behaviour. In your case for loop termination condition is a[i] !=0 which is happening after 12 elements after crossing array bounds.

Comments

1

You access beyond the array length, therefore you can't predict the value after the program writes more than 5 members to the output (where i > 4 ) and that is why you program results in undefined behavior. The program will run until it accidentally encounters a value of i where a[i] is equal to 0.

you might fix the code like this

unsigned length = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < length ;i++){
      cout << a[i] << endl;
}

Comments

0

The condition of loop stop is a[I], equals to a[I] is zero. So actually you have exceeded the boundary of array and outputted some dirty data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.