Code says int j=1; debugger says that j=3 (C++)

66 views Asked by At

Honestly the problem is really simple yet I don't know how to fix it. In my program the line says int j=1; yet the debugger says that it equals 3. I even took int j out of the loop to highlight the issue. Is this some weird compiler optimization or something?

Debugger on the left and line highlighted:

Debugger on the left and line highlighted

I tried to pull j out of the loop to see if anything changed and it didn't. This is for a leetcode problem (Jump Game) where my method was to see if there were any zeroes in the nums array, and if there was a zero, see if there is a value that can jump over it considering the index of that value and the index of the zero. The program worked for most cases but failed if nums = {2, 0, 0}.

1

There are 1 answers

1
user12002570 On

Is this some weird compiler optimization or something?

You've not executed that statement yet. After you execute it, j will become 1

The highlighted line shows what statement is to be executed next. Not what has been executed.