2

I am a bit puzzled, since it seems that the C++ Debugger in VS2010 is behaving a bit oddly. If I go about and run this:

int i = 100;

for(int i = 0; i < 5; i++)
{
    printf("Value of i inside loop: %d", i);
}

printf("Value of i outside loop: %d", i);

then, when breakpointing on a line after the last one above and hovering the cursor above the "i" variable, the debugger shows the value 5.

However, if I decide to send the "i" variable as a parameter to a method:

Test(100);

void Test(int i)
{
    for(int i = 0; i < 5; i++)
    {
        printf("Value of i inside loop: %d", i);
    }

    printf("Value of i outside loop: %d", i);
}

then, when breakpointing on the last line and hover with the mouse on "i", I, the debugger shows the value 100.

Could anyone enlight me on this (or test on your machine). Is it a bug or a feature or am I missing something?

Thanks in advance!

UPDATE: just to make things clear - actual program prints and executes as intended, it is only the debugger that shows unexpected values. So, one can ignore it says "printf", it could have been almost anything involving the variable "i".

6
  • Are you in debug or release? Commented Mar 24, 2014 at 12:51
  • Also, debuggers can lie Commented Mar 24, 2014 at 12:53
  • @OMGtechy Sorry forgot to mention, I am in Debug (x64 at the moment) :) Commented Mar 24, 2014 at 12:53
  • @OMGtechy Yes, seems like debuggers can be real buggers! Commented Mar 24, 2014 at 12:55
  • Try changing the i in the for loop to something else, like counter. I think this should sort it for you. Commented Mar 24, 2014 at 12:55

3 Answers 3

3

It's a bug. You are asking the debugger to tell you the value of a variable, i. There are two variables of that name in the current stack frame. The debugger is giving you the wrong one (which is out of scope). This is probably because local variable debug symbols are not generated for each scope, only for each stack frame.

Don't do that, it's a really bad idea. It will confuse you, or if not you, a programmer who comes after you, just as it confused the debugger.

It should generate warning C6244.

It is best for new projects to specify /Wall /WX, that is, Enable All Warnings and Treat Warnings as Errors.

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

1 Comment

Thanks for the reply! Yep, it is not my code - I am the programmer who came after someone else :) I simply found this interesting from an academical point of view and was curious if I had missed something or if it was a bug. I know it's ugly, I just thougt that since that code is valid, it is strange that the debugger behaved differently than actual execution of the code.
2

In both the cases there are 2 declarations of i. In the first case the declaration i=100; is ignored by c++ compiler so you get i=5 when loop ends. In the second case when function ends the value of i is the same as that of parameter because you are only declaring i once within the function. and when loop ends the scope of i in the loop ends and the value passed as parameter is kept in i. In second case no definition is ignored.

1 Comment

Thank you for your reply! But c++ cannot ignore that i=100, since it actually prints the value correctly (only debugger shows wrong value). That code is uglu but legit, since the for-loop has its own scope which should hide the outer-scope variable "i".
-1

The first one is a bug. The printf should print 100 either way. Some versions of VS have an option 'enforce for-loop scope conformance' or some such.

3 Comments

It prints correctly. As I wrote in my first post it is simply the debugger that shows a wrong value when breakpointing.
Reluctant to down-vote you immediately but the variable name can be re-used in the scope of the for-loop, hiding the original variable. If you know differently I'd be interested to know how you see it.
RobH: The scope of a variable declared in a for-loop ends after the for-loop, thus 'unhiding' the previous 'i'

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.