不知道为什么我遇到堆栈溢出
Don't know why I am getting a stack overflow
我不明白为什么当我进入main函数时我立即得到堆栈溢出。我应该从文本文件中读取并做一些处理。有人能给我解释一下原因并建议如何解决吗?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX=100;
enum countrytype{S,F};
struct dob
{
int day;
int month;
int year;
};
struct Local
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];
};
struct Foreign
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];
};
union Student
{
Local localstudent;
Foreign foreignstudent;
};
struct UOWstudent
{
countrytype ct;
Student st;
};
void readfile(ifstream &read,UOWstudent noofstudent[MAX]);
int main()
{
UOWstudent noofstudent[MAX];
ifstream read;
readfile(read,noofstudent);
cout<<endl
<<noofstudent[0].st.foreignstudent.country
<<endl
<<noofstudent[0].st.foreignstudent.gender
<<endl
<<noofstudent[0].st.foreignstudent.name;
system("PAUSE");
}
void readfile(ifstream &read, UOWstudent noofstudent[MAX])
{
int i=0;
char country;
char filename[MAX];
cin>>filename;
read.open(filename);
read>>country;
/*if (country =='F')
{
read.getline(noofstudent[i].st.foreignstudent.country,MAX);
read>>noofstudent[i].st.foreignstudent.gender;
read.getline(noofstudent[i].st.foreignstudent.name,MAX);
}
else
read.getline(noofstudent[i].st.foreignstudent.country,MAX);*/
}
这是我的文本文件
F South Korea
Male Psy Park Jae Sang
31 - 12 -1977
3 CSCI114 55 CSCI103 44 GangNam
简单地说,您的代码正在分配堆栈上的所有存储空间,并且您分配的存储空间超过了允许的限制。
看看为什么你超过了限制可能更有用。
main()
的第一行是在堆栈上分配一个100 (MAX = 100)个学生的数组:
UOWstudent noofstudent[MAX];
一个UOWstudent
有多大?您可以通过查看每个字段来确定:
struct UOWstudent
{
countrytype ct; // enum. let's assume 4 bytes. (32-bit executable)
Student st; // ???
};
一个学生有多大?
union Student
{
Local localstudent;
Foreign foreignstudent;
};
这是一个本地或外国的大小,所以我们只看一个。我们需要对char的大小做另一个假设。假设1 byte
(8位字符):
struct Local
{
char country[MAX]; // 100 bytes
char gender[MAX]; // 100 bytes
char name[MAX]; // 100 bytes
dob birthday; // 3 ints or 12 bytes (32-bit assumption again)
int noofmod; // 4 bytes
char mod[MAX][MAX]; // 10,000 bytes
int mark[MAX]; // 400 bytes
}; // total: 10,716 bytes
所以main()的第一行尝试在堆栈上分配(10,716 + 4)x 100 = 1,072,000字节。我对char和int的大小做了最保守的假设,它们很可能更大。如果堆栈限制确实是1兆字节(1,048,576字节),那么这个初始分配就超过了限制。
可以使用C语言的sizeof操作符来获取有关类型实际大小的信息。参考这个stackoverflow答案,讨论在堆上而不是堆栈上分配数组,这是解决问题的一个很好的步骤。(UOWstudent == University of Waterloo student?)
MAX被定义为100(它真的需要吗?),你有一堆字符数组MAX元素的长度,你甚至有一个2D数组,这是巨大的。你的结构体很大,可能超过了最大堆栈大小——我想在windows中是1024Kb。
相关文章:
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- C++ 对象数组堆栈溢出
- 有没有一种方法可以捕获进程中的堆栈溢出?C++Linux
- 对象接收堆栈溢出异常 c++ 的排序向量
- 将公共递归转换为尾递归,因为大型输入的堆栈溢出
- C++ 中递归期间的堆栈溢出
- 启动 dll 时 C# 环境堆栈溢出
- 在C++中使用数组时如何防止堆栈溢出?
- 如何修复递归函数导致的堆栈溢出错误?C++
- 当我尝试为结构分配新指针时出现堆栈溢出错误
- 为什么析构函数无休止地调用自己(导致堆栈溢出)?
- 为什么堆栈溢出?如有建议,不胜感激
- 主函数执行时C++堆栈溢出异常
- 如何在不导致堆栈溢出的情况下计算非常大的数字和很小的 HCF.我正在使用欧几里得算法
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 如何在Windows上报告堆栈缓冲区溢出
- 如何抑制来自 gcc 中地址清理器的堆栈缓冲区溢出
- 声明大数组时堆栈/堆溢出
- c++中栈溢出和分段错误的危险