0

what is the problem of this snippet code and why this is like that!? i think the stack overflows here but somone says its '0'.

https://code.sololearn.com/cW5dBl45S9RY #include<iostream> int main(){ int a[10],i; for( i=4;i<9;i+=3) {a[i]=i-2;} std::cout<<a[8]; }

11th Jun 2022, 7:51 AM
Nariman Tajari
Nariman Tajari - avatar
4 Answers
+ 4
The problem is that the array is not initialised. Only a[4] and a[7] are set. For a[8], any value is possible, since memory space does not get leared for local arrays.
11th Jun 2022, 8:15 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 3
ok Ani Jona šŸ•Š you mean the value would be change each time one compile the code?
11th Jun 2022, 8:24 AM
Nariman Tajari
Nariman Tajari - avatar
+ 1
Not exactly each compile but each run of the program (if you wouldnā€™t compile before each run)
13th Jun 2022, 7:27 AM
JayV
0
You're right. I misunderstood it to mean one compilation, change each run. But i guess it means each time the code is compiled. No. Opposed to static or global arrays, there is no information about the array content compiled into the code. The content is whatever was left in the memory area your program is mapped to upon start. And that may change each time it is run.
13th Jun 2022, 7:36 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar