Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this local variable shadowing/hiding another normal or a bug in Visual Studio?

I've greatly simplified this question as the same problem arises in a simpler case:

#include <iostream>

int height;    
int main()
{
    std::cout << height; // Visual Studio debugger shows this value as -858993460    
    int height;
}

enter image description here

Seems a problem with the debugger displaying the wrong variable value. The variable value is correct, as printing the variable shows the correct global height value, 0.

like image 789
Zebrafish Avatar asked Feb 02 '18 07:02

Zebrafish


1 Answers

You are correct, the global variable height is not shadowed until the declaration of the automatic variable height in the final statement of main().

std::cout << height; will use the global variable height.

Yes, this is confusing the debugger. It's displaying the value of the local height variable, which in a debug build is initialized to 0xCCCCCCCC, or the -858993460 displayed in decimal mode.

The compiler does the correct thing and fetches the global variable height in the first line of the function, it's just the debugger that is confused.

like image 182
5 revs, 2 users 80% Avatar answered Oct 17 '22 00:10

5 revs, 2 users 80%