gpt4 book ai didi

c++ - 从文件中读取 31,622,400 个字符时出现段错误

转载 作者:太空狗 更新时间:2023-10-29 23:31:13 25 4
gpt4 key购买 nike

这是源代码:

int main() {
int secondsInYear = 366*24*60*60; // Equals 31,622,400
short int data[secondsInYear];
FILE * pFile;
pFile = fopen ("stat", "r");
fread(data, sizeof(short int), secondsInYear, pFile);
fclose(pFile);
}

在线 fopen("stat", "r") 它给我段错误!如果我读取 secondsInYear/10 字符,它将毫无问题地执行,那么问题似乎是什么?解决方案是什么?

最佳答案

您正在堆栈上创建一个巨大的数组。所以你遇到了计算器溢出。 :)

您应该改为动态分配该数组。

short int *data = new short int[secondsInYear];

并确保稍后将其删除:

delete[] data;

关于c++ - 从文件中读取 31,622,400 个字符时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7773607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com