Posts

Showing posts with the label initialization

Range of indexes not default initialized in arrays in C++

Range of indexes not default initialized in arrays in C++ The following is the code I am trying to run #include<bits/stdc++.h> using namespace std; int main() { bool x[101010]; for(int i=0;i<101010;i++) { if(x[i]) cout<<i<<" "; } return 0; } As far as I know, the default value of boolean type variables is false . However, for the above code from index 94758-101008 value of i is being printed which means they are default initialized as true. Can anyone please help me in figuring out where am I going wrong? 2 Answers 2 Your problem can be reduced to this: bool x; std::cout << x; A boolean is a fundamental type. Default initializing automatic variables of a fundamental type leaves them with indeterminate values. Not false , but indeterminate. Using those values leads to undefined behavior. This is what you are seeing. false ...