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? :|
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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? :|
Name |
---|
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.