简体   繁体   English

如何调试导致堆栈溢出错误的递归 function?

[英]How can I debug my recursive function that's causing stack overflow error?

The problem I'm trying to solve is KR2 exercise 1-22;我要解决的问题是 KR2 练习 1-22; 'write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. '编写一个程序,在第 n 列输入之前出现的最后一个非空白字符之后将长输入行“折叠”成两行或多行较短的行。 Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column'.确保你的程序用很长的行做一些智能的事情,如果在指定的列之前没有空格或制表符。

I've attempted to do with a recursive function which just gets stuck.我试图处理一个递归的 function ,它只是卡住了。

Example of erroneous behavior:错误行为示例:
hello world << input你好世界<<输入
output >> output >>
hell地狱
o wo喔喔
o wo喔喔
o wo喔喔
o wo喔喔
.... ……
... ...
Expected behavior:预期行为:
hello world << input你好世界<<输入
output >> output >>
hell地狱
o wo喔喔
rld rld

Second example of erroneous behavior:错误行为的第二个例子:
he llo world << input他 llo 世界 << 输入
output >> output >>
he
llo
llo llo咯咯咯
llo llo咯咯咯
llo
.... ……
... ...
Secondary expected behavior:次要预期行为:
he llo world << input他 llo 世界 << 输入
output >> output >>
he
llo
wor工作
ld ld

See code below:请参见下面的代码:

#include <stdio.h>
#define PAGEWIDTH 5
#define MAXLINE 1000

int getline(char line[], int maxline);
void fold(char line[], int start);

/* "fold" long input lines into two or more shorter lines after
  the last non-blnk character that occurs before the n-th
  column of input */
int main()
{
    int len;                /* current line length*/
    char line[MAXLINE];     /* current input lne*/

    while ((len = getline(line, MAXLINE)) > 0)
        if (len > PAGEWIDTH)
            fold(line, 0);
    return 0;
}

/* getline : read a line into s, return length */
int getline(char s[], int lim)
{
    int c, i;

    c = 0;
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';

    return i;
}

void fold(char line[], int start)
{
    int i, lstnb;
    i = lstnb = 0;

    for (i = 0; i < PAGEWIDTH-1 && line[start + i] != '\0'; ++i) {
        if (line[start + i] != ' ')
            lstnb = i;
    }
    for (i = 0; i <= lstnb; ++i)
        putchar(line[start + i]);

    putchar('\n');
    if(line[lstnb + 1] != '\0')
        fold(line, lstnb + 1);
}

good question.好问题。 First, I think that a recursive solution isn't ideal here.首先,我认为递归解决方案在这里并不理想。 I got it working for the inputs you gave, but considering how small PAGEWIDTH is, you'll likely run into issues with larger inputs.我让它适用于您提供的输入,但考虑到PAGEWIDTH有多小,您可能会遇到较大输入的问题。 Using a loop would make your life easier and result in a more reliable function.使用循环将使您的生活更轻松,并产生更可靠的 function。

That said, your issue lies in the recursive call you make in the fold() function, you are providing the wrong start index.也就是说,您的问题在于您在fold() function 中进行的递归调用,您提供了错误的起始索引。 This means you don't move forward in the line with each call.这意味着您不会在每次通话时都继续前进。 Also, you don't clearly define a base case (ie when to end the recursion).此外,您没有明确定义基本情况(即何时结束递归)。 You can do this by modifying your for loop conditions or passing the string length (n) to the function and checking whether start > n.您可以通过修改 for 循环条件或将字符串长度 (n) 传递给 function 并检查是否 start > n 来做到这一点。

Checking base case and the recursive call is always a good place to start when debugging recursive functions.在调试递归函数时,检查基本情况和递归调用始终是一个很好的起点。 This is normally where my issues come from and it's really easy to assume you did them correctly and overlook them while debugging.这通常是我的问题的来源,很容易假设您正确地完成了它们并在调试时忽略了它们。

I'm not sure what you are looking for in terms of an answer, but if those suggestions don't get you any closer, I pasted a fix in a pastebin here .我不确定您在寻找什么答案,但如果这些建议没有让您更接近,我在此处的粘贴箱中粘贴了一个修复程序。 I didn't post it here in case you wanted to solve it on your own.我没有在这里发布它以防你想自己解决它。

Hope that works, let me know if there's something I missed.希望这有效,如果我错过了什么,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM