Reputation: 33
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
Upvotes: 3
Views: 1083
Reputation: 108986
The debugger executes statements.
The line in question does not have a full statement; the debugger never stops at the middle of a statement.
Imagine you have the multiple line statement like below. Where would the debugger stop?
for (
i = 0;
i < 100;
i++)
sum += a[i];
Upvotes: 0
Reputation:
Your if is redundant. It's the last thing in the loop, and all it does is continues the loop. If you use any optimization whatsoever, it may be removed.
What is your code supposed to do?
Upvotes: 0
Reputation: 22114
The continue would take you to while statement also. This is perhaps due to an optimization by the compiler. It detected that there are no statements following the continue and hence the resulting step would be same whether the if condition evaluated to true or not.
Upvotes: 0
Reputation: 13624
If you are compiling with optimizations enabled, it is entirely possible that the jump instruction for the if
is simply going straight to the while
, rather than bothering with the continue
, which would just be another simple jump instruction.
Upvotes: 1