C++: Uninitialized variables garbage

前端 未结 5 1045
攒了一身酷
攒了一身酷 2020-12-12 02:38
int myInt;
cout << myInt; // Garbage like 429948, etc

If I output and/or work with uninitialized variables in C++, what are their assumed val

5条回答
  •  旧巷少年郎
    2020-12-12 03:23

    Its value is indeterminate. (§8.5/9)

    There's no use trying to get meaningful data from it. In practice, it's just whatever happened to be there.

    Most compilers will pack "meaningful" debug data in there in a debug build. For example, MSVC will initialize things to 0xCCCCCCCC. This is removed in an optimized build, of course.

提交回复
热议问题