繁体   English   中英

两个连续的fget段故障

[英]two consecutive fgets seg faults

我目前正在尝试读取两个字符串s和t,这些字符串将输入到stdio中。 它们将在单独的行中输入。

以下代码段错误。

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

int main()
{
    char t[5000000];
    char s[5000000];

    fgets(t,50000,stdin);
    fgets(s,50000,stdin);

    printf("%c",t[1]);


}

但是,单个fgets不会。

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

int main()
{
    char t[5000000];
    char s[5000000];

    fgets(t,50000,stdin);

    printf("%c",t[1]);

}

其他帖子讨论了一些返回和“ / n”问题,但我不明白问题到底出在哪里。

数组太大,无法在堆栈上声明它们,它会填满并发生堆栈溢出,要么使用malloc在堆上声明它们,要么使它们变小。

声明它们为static也将使其工作,因为静态变量存储在内存中的其他位置而不是堆栈中。

仅供参考:

堆栈大小因平台而异,在Linux平台上,堆栈大小默认为8MB。 您可以使用系统调用getrlimit()和setrlimit()对其进行更改并根据需要进行更改。

暂无
暂无

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

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