Hello! :)
One of my friends(kia.masster) submitted a problem with these two codes:
The only difference is the "cerr" line. Why has this happened? :|
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hello! :)
One of my friends(kia.masster) submitted a problem with these two codes:
The only difference is the "cerr" line. Why has this happened? :|
Название |
---|
Variable "s" was not initialized. This led to undefined behavior.
Hello! :)
1- It hasn't been initialized in second code also. 2- Where did you get this error? Be sure to compile it by GNU C++.
Hello there!
Yes, it wasn't initialized in both codes. But I said it led to undefined behavior, not 100% wrong answer. If s is automatically initialized by 0 by compilator then your solution passes. If it is initialized by something else — bad luck:(.
And I compiled this code by my Code:Blocks Gnu compiler. And the output was 0 on debug and 1 on release because of this problem.
s has a (pseudo) random value depending on the state of memory before using address X for variable s. The memory is not initialized at address X, so it still contains the value of the variable that was in that place before. So it depends on such strange things like calling printing routines. Printing routines use the memory, then mark it as free. But the memory is not usually cleared, it still contains the old values.
This regards to local variables. Global variables are initialized to 0.