简体   繁体   中英

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;
}

在此输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM