Can anyone explain why does the first code get Runtime Error?
Thanks!
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3741 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3489 |
7 | Radewoosh | 3483 |
8 | Kevin114514 | 3442 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
2 | atcoder_official | 162 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
Can anyone explain why does the first code get Runtime Error?
Thanks!
Name |
---|
I tried debugging it and it seems to have something to do with the order of the equalization, and the vector usage. You cannot use [] operators when the size of vector is 0. So when we use in the first call reg[] we are trying to access an vector of size 0, which causes an error.
The problem ceases when we resize the vector to size 1. Here.
Or in your second example when we resize the vector using the function, before trying to access the vector itself.
Thank you for your reply!
But in this code I'm not using [] operator with an empty vector, and still have a runtime error?
The thing is: you use invalid memory when assigning to reg[x], because the function, which result is assigned to reg[x], does change the vector (and, possibly, it's allocation). See this comment for detailed explanation.
I suspect that function getRegIndex might cause vector relocation in memory, so reg[getRegIndex(1,reg)] won't be part of your vector anymore.
I think you are right, here I increase the capacity before calling the function and there is no problem.
Thank you!