I have a problem with going over an if statement that the code should enter in c:
void getInput(void)
{
static size_t _read = 0;
memset(line, 0, _read);
do{
memset(line, 0, _read);
write(STDOUT_FILENO, ">> ", 3);
_read = read(STDIN_FILENO, line, MAXLINE - 1);
if (line[0] == '\n' || line[0] == '\r')
continue;
}while(line[_read - 1] != '\n');
line[_read] = '\0';
}
The problem is at the line “if(line[0] == ‘\n’ || line[0] == ‘\r’)” here is an extract from the debugger.
getInput () at main.c:29
29 if (line[0] == '\n' || line[0] == '\r')
1: line[0] = 10 '\n'
(gdb) step
31 }while(line[_read - 1] != '\n');
1: line[0] = 10 '\n'
(gdb) list 29
24 memset(line, 0, _read);
25 do{
26 memset(line, 0, _read);
27 write(STDOUT_FILENO, ">> ", 3);
28 _read = read(STDIN_FILENO, line, MAXLINE - 1);
29 if (line[0] == '\n' || line[0] == '\r')
30 continue;
31 }while(line[_read - 1] != '\n');
32 line[_read] = '\0';
33 }
As you can see it does not go to the continue statement, but rather straight to the while statement. I did think that the continue would go to the while statement but i still dont understand why it doesnt step on the continue first.
Thanks
If you are compiling with optimizations enabled, it is entirely possible that the jump instruction for the
ifis simply going straight to thewhile, rather than bothering with thecontinue, which would just be another simple jump instruction.