代码在我的系统上运行良好,但当我将其提交给应该检查它的机器人时,会导致堆栈崩溃错误

标签 c stack

这个阶段的程序应该从用户那里获取文本并将其分成段落、句子和单词。到目前为止,代码在我的系统(ubuntu 18.04)上运行良好,但是当我将其提交给应该向其提供输入的机器人时,会出现堆栈粉碎错误。有没有办法让我的代码读取输入而不崩溃?

编辑: 这是一个测试输入。我认为问题在于它一次读取所有内容。(除了 ap: 之外还有一些其他选项,我还没有制作):

测试 3 输入:ap:小说的中心是典型的 格雷厄姆·格林角色。[
]fw:h[
]fs:ovel[ ]fp:典型[
]owf[
]owl[
]ap:He累了 持怀疑态度,基本上正派但愤世嫉俗。一种感官
生活对他来说没有真正的色彩,事实上它让他感到无聊,然而 存在着某种救赎、某些人的潜在希望 意义感。[
]fw:or[
]fw:is[
] owf[
]owl[
]qt

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ap(char ***p, char ***s, char ***w, int *n_p, int *n_s, int *n_w)
{
    char *temp = malloc(10001 * sizeof(char));
    int i, k, j, length;

    if(temp == NULL)
    {
        printf("Could not allocate memory");
        return;
    }
    fgets(temp, 10001, stdin);
    while(temp[0] == ' ')
        for(i = 0;i < strlen(temp);i++)
            temp[i] = temp[i + 1];

    //paragraphs
    *n_p += 1;
    **p = realloc(**p, *n_p * sizeof(char *));
    *(*p + *n_p - 1) = malloc((strlen(temp) + 1) * sizeof(char));
    if(**p == NULL || *(*p + *n_p - 1) == NULL)
    {
        printf("Could not allocate memory");
        return;
    }
    strcpy(*(*p + *n_p - 1), temp);

    //sentences
    while(temp[0] != '\0')
    {
        k = strcspn(temp, ".!?;");
        length = strlen(temp);
        temp[k] = '\0';
        *n_s += 1;
        **s = realloc(**s, *n_s * sizeof(char *));
        *(*s + *n_s - 1) = malloc((strlen(temp) + 1) * sizeof(char));
        if(**s == NULL || *(*s + *n_s - 1) == NULL)
        {
            printf("Could not allocate memory");
            return;
        }
        strcpy(*(*s + *n_s - 1), temp);
        j = 0;
        for(i = k + 1;j <= length - k;i++)
        {
            temp[j] = temp[i];
            j++;
        }
        while(temp[0] == ' ')
            for(i = 0;i < strlen(temp);i++)
                temp[i] = temp[i + 1];
        k = strcspn(temp, "\n");
        while(temp[k + 1] == ' ')
            for(i = k;i < strlen(temp);i++)
                temp[i] == temp[i + 1];
        if(!(strcmp(*(*s + *n_s - 1), "\n")))
        {
            free(*(*s + *n_s - 1));
            *n_s -= 1;
        }
     }
}

int main()
{
    char **paragraphs, **sentences, **words, option[5];
    int num_par = 0, num_sent = 0, num_words = 0, i;

    paragraphs = malloc(sizeof(char *));
    sentences = malloc(sizeof(char *));
    words = malloc(sizeof(char *));
    if(paragraphs == NULL || sentences == NULL || words == NULL)
    {
        printf("Could not allocate memory");
        return 1;
    }

    do
    {
        scanf("%s", option);
        if(!(strcmp(option, "ap:")))
            ap(&paragraphs, &sentences, &words, &num_par, &num_sent, &num_words);
    }
    while(strcmp(option, "qt"));

    //test
    for(i = 0;i < num_par;i++)
        printf("%s", paragraphs[i]);
    printf("-------------  %d\n", num_sent);
    for(i = 0;i < num_sent;i++)
        printf("%s\n", sentences[i]);

    return 0;
}

最佳答案

选项太小,该行:

scanf("%s", option);

尝试将每一行读入 5 个字符的数组中。超过 5 个字符的行会溢出数组,gcc 会用金丝雀捕获它,并因堆栈粉碎而杀死你。

如果您的本地编译器未实现堆栈粉碎检测,它可能会运行。

char option[x] 增加到适合行长度的某个合理的 x 值。

关于代码在我的系统上运行良好,但当我将其提交给应该检查它的机器人时,会导致堆栈崩溃错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509457/

相关文章:

c - wait(NULL) 定义明确吗?

c - Shellcoder手册: first shellcode example

C#:顺序函数调用如何工作? (效率方面)

python - 如何增加python中的堆栈大小

c - 如何打印内存和缓冲区的长度?

c - #define是否需要设置数据类型?

c - 适用于 Windows 的轻量级 C HTTP 客户端

c - for (unsigned char i = 0; i<=0xff; i++) 产生无限循环

ios - 水平堆栈 View 新行 - Swift

c - 将每一行strdup放入数组?