Hi all,
I would like to share something which happened with me in today's contest. I solved the following problem: Strong Password. Here is my submission link: Submission.
I was not sure why it was giving runtime error, and after the contest, I tried submitting it in C++20. It passed without any problem! Here is the code snippet which was the reason of runtime error:
string s;
cin >> s;
...
vvll post(s.size(), vll(10, -1));
...
for (ll i = s.size() - 2; i >= 0; --i) {
for (int j = 0; j < 10; ++j)
post[i][j] = post[i + 1][j];
...
}
vvll
is vector<vector<long long>>
, vll
is vector<long long>
and ll
is long long
.
Can you identify the source of runtime error here? Hint: what if input string is "0"
?
Reason of failure in C++ 17 (32 bit compiler):
s.size() - 2
should semantically result in-1
and data type ofsize_t
is unsigned integer of $$$4$$$ bytes.- This computation results in
0xFFFFFFFF
($$$4$$$ bytes, unsigned number).
- This computation results in
- LHS is of data type signed long long, which is $$$8$$$ bytes.
- LHS can safely handle consecutive $$$32$$$ bits which are all set to $$$1$$$.
- Thus, $$$i$$$ will be set to
0x00000000 FFFFFFFF
which is $$$4294967295$$$.
Reason of above code passing in C++ 17 (64 bit compiler):
s.size() - 2
should semantically result in $$$-1$$$ and data type ofsize_t
is unsigned integer of $$$8$$$ bytes.- This computation results in
0xFFFFFFFF FFFFFFFF
($$$8$$$ bytes, unsigned number).
- This computation results in
- LHS is of data type signed long long, which is $$$8$$$ bytes.
- LHS can't handle consecutive $$$64$$$ bits which are all set to $$$1$$$.
- That value of $$$i$$$ is implementation defined and we can all agree that most of the implementations will simply treat this number as signed one, without any change in data.
- Thus, $$$i$$$ will be set to
0xFFFFFFFF FFFFFFFF
which is $$$-1$$$ and the code works as expected.