I wrote a piece of code as shown below, I now know that when i = -1
, the error “addition of unsigned offset” will occur.
std::string path = "/home/";
int i = 0;
while (path[i] == '/') { // while (i >= 0 && path[i] == '/') {
--i;
}
This code throws an error on LeetCode, but runs fine on my vscode. I tried to monitor the variable information with gdb, and I found that path[-1] = '00'
.
I’m wondering why my compiler doesn’t throw this error? My g++ version is 13.2.0, gdb version is 14.2, cppStandard is gnu++14. How should I adjust the compiler?