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 | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 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.