6

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Zebrafish
  • 11,682
  • 3
  • 43
  • 119

1 Answers1

8

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.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Yes... I've just found out that it's the debugger which shows the false value of the local. If I std::cout it seems to print the right value. Ohhh, this is so frustrating. I'm sorry. I should delete this question, it's just a misleading Visual Studio debugger thing. – Zebrafish Feb 02 '18 at 08:05
  • @Zebrafish: No, it's a good question and one that actually requires you to paste up the image. It might be an idea to simplify further and submit a bug report to MS. – Bathsheba Feb 02 '18 at 08:09
  • I agree, I just tried it in VS2017 and it is definitely a bug in the debugger. If you could report the bug to MS that would be great, and keep the question here in case anyone else runs into it. – Michael Geary Feb 02 '18 at 08:34
  • @Zebrafish: I've amended the answer to "catch up" with the question edits. – Bathsheba Feb 02 '18 at 08:36
  • @MichaelGeary: I've wiki'd this so other folk can freely make edits to the answer, as we all explore how we can best demonstrate this to MS. – Bathsheba Feb 02 '18 at 08:43