The following code works, but is it well defined?
const int N = 8;
int arr[N];
int* ptr = arr;
for(int i=0; i<N; i++) {
std::cout << ptr[i] << "\n";
}
and a second question:
running this code with -O2 optimizations compiled as C++14 http://cpp.sh/7eagy gives me the following output:
1
0
0
0
4196448
0
4196198
0
wherease the following version:
const int N = 8;
int arr[N] = {};
int* ptr = arr;
for(int i=0; i<N; i++) {
std::cout << ptr[i] << "\n";
}
gives
0
0
0
0
0
0
0
0
indicating that without the = {} arr was not zero initialized, but according to https://en.cppreference.com/w/cpp/language/zero_initialization it should be
at least the given example states: double f[3]; // zero-initialized to three 0.0's
so what's the reason I'm not getting zeros in the first version?
int arr[N];is declared but not defined. However, in the second version it is declared and initialized to 0 explicitly.