For遍历数组导致无限循环 [英] For loop through array causing infinite loop
问题描述
我有一段代码应该在按下回车键时刷新一个字符数组,只有代码无限循环,我不知道为什么。
I have a piece of code that is supposed to flush an array of characters when the enter key is pressed, only the code is looping infinitely and I can't figure out why.
有一个字符数组,每次按下一个键都会获取一个新字符。当检测到回车键时,将执行以下代码,以在屏幕上生成新行之前用 \0个字符刷新字符数组:
There is a character array that gets a new character every time a key is pressed. When the enter key is detected, the following code gets executed to flush the character array with '\0' characters before generating a new line on the screen:
int main()
{
char i;
char c;
char buffer[80];
i = 0;
c = 0;
while (c = bgetchar())
{
if (c == 13)
{
for (i = i; i >= 0; i--)
{
buffer[i] = '\0';
}
}
else
{
buffer[i] = c;
i++;
}
}
}
我在每次按下字符时都会进入主循环(输入(ASCII 13)除外)。该字符也被添加到buffer []。尽管我可以重现它,但是这里没有显示该代码。不用说,即使代码不正确地增加了i,循环也应该在我达到0时结束(并且我应该在递减的某个时候达到0)。
The i gets incremented in the main loop every time a character gets pressed (other than an enter (ASCII 13)). The character is also added to buffer[]. That code is not shown here, though I can reproduce it. Needless to say, even if that code were improperly incrementing the i, the loop should end when i hits 0 (and i should hit 0 at some point given the decrement).
因此,从理论上讲,按下Enter键后,如果该行上有5个字符(并且没有退格键),则buffer []元素0-4将具有这些字符,而i将等于5。代码中的循环应该用'\0'字符替换元素5到0。从元素5开始当然是不必要的,但这不应引起我一直在经历的行为。
So in theory, on having an enter pressed, if there are 5 characters on the line (and no backspaces), buffer[] elements 0-4 will have the characters, and i will be equal to 5. The loop in the code should replace elements 5 to 0 with '\0' characters. Starting with element 5 is admittedly unnecessary, but it shouldn't cause the behavior I've been experiencing.
这种行为是循环不断地运行。现在,我可以通过每次循环运行时使用putchar函数打印一个字符来验证这一点。现在,我想我可以编写另一个函数以使我也可以打印i的值,并且我很可能会得到我问题的答案。同样,就其价值而言,当我将这个循环变成等效的 while循环时,就会发生无限循环。
That behavior is that the loop runs endlessly. Right now I've been able to verify this by using my putchar function to print a character every time the loop is run. Now I suppose I could write another function to enable me to print the value of i as well, and I would probably obtain the answer to my question. Also, for what it's worth, the infinite loop happens when I turn this loop into the equivalent "while" loop.
我只是想确保没有任何东西很明显,这将导致无限循环行为。抱歉,这是一个愚蠢的问题。谢谢你的帮助。该程序以16位实模式运行为原始二进制文件。我正在从bcc到as86到最终的二进制文件进行编译。
I just wanted to make sure there isn't something obvious that would be causing infinite loop behavior here. Sorry if this is a stupid question. Thanks for any help. The program is being run as a raw binary in 16 bit real mode. I'm compiling from bcc to as86 to the final binary.
编辑:经过更多调试后,我确认我将使用正确的正值进入循环(基于数组中的字符数,例如,如果7个字符,则i = 7)。但是,在for循环中,i无限地在-1和-2之间切换。由于基于i的输入值(或基于循环的条件)没有意义,因此这是否存在某种内存或寄存器问题?
After doing some more debugging, I confirmed that i is going into the loop with the correct positive value (based on the number of characters in the array, e.g., i = 7 if 7 characters). While in the for loop, however, i alternates between a value of -1 and -2 ad infinitum. Since that makes no sense based on i's entry value (or based on the loop's condition), is there any chance this is some kind of memory or register issue?
最后编辑:问题似乎是导致我变为负面的> =条件。由于某种原因,即使for循环中的条件检查负i,这也会导致无限循环。感谢您的所有帮助。
Final edit: the issue appears to have been the >= condition causing i to go negative. For some reason, this caused an infinite loop even though the condition in the for loop checks for a negative i. Thanks for all of the help.
推荐答案
由于某种原因,即使for循环中的条件检查,也会导致无限循环对于否定的i。
For some reason, this caused an infinite loop even though the condition in the for loop checks for a negative i.
for循环检查中的条件为true,但是并没有在需要的地方进行条件检查。
"the condition in the for loop checks" is true, yet the condition is not checked everywhere it is needed.
至少一个问题:尝试 buffer [-1] = c;
是未定义行为(UB)-可能包含无限循环。
At least one problem: attempting buffer[-1] = c;
which is undefined behavior (UB) - which can include an infinite loop.
在输入之后 (c == 13)
不是13,代码尝试此UB。
When the input after (c == 13)
is not 13, code attempts this UB.
while (c = bgetchar()) {
if (c == 13) {
for (i = i; i >= 0; i--) {
buffer[i] = '\0';
}
// now i == -1;
} else {
// No protection here that `i` is on the 0..79 range
buffer[i] = c;
i++;
}
}
for(i = i; i> = 0; i-)
有疑问,因为它的第一个 buffer [i] ='question0' ;
在else的 buffer [i] = c中未设置的内容上; i ++;
部分。
for (i = i; i >= 0; i--)
is questionable as its first buffer[i] = '\0';
is on something that was not set in the else's buffer[i] = c; i++;
part.
我怀疑OP需求
// for (i = i; i >= 0; i--) {
for (i = i; i > 0; ) {
// buffer[i] = '\0';
buffer[--i] = '\0';
}
char i;
是不寻常的。我希望使用固定的签名类型,而不是可能是签名或未签名的 char
。也许 unsigned char i
(在修复后)或惯用的 size_t i;
用于数组索引。
char i;
is unusual. I'd expect a fixed signed type, not a char
which might be signed or unsigned. Perhaps unsigned char i
(after above fix) or an idiomatic size_t i;
for array indexing.
这篇关于For遍历数组导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!