问题:为什么在函数体中定义的内置类型的未初始化对象具有未定义的值,而在任何函数之外定义的内置类型的对象都初始化为0或“?”
举个例子:
#include <iostream> using std::cout; using std::endl; int ia[10]; /* ia has global scope */ int main() { int ia2[10]; /* ia2 has block scope */ for (const auto& i : ia) cout << i << " "; /* Result: 0 0 0 0 0 0 0 0 0 0 */ cout << endl; for (const auto& i : ia2) cout << i << " "; /* Result: 1972896424 2686716 1972303058 8 1972310414 1972310370 1076588592 0 0 0 */ return 0; }