C++ Don';我不知道为什么堆栈溢出
我不明白为什么当我进入主函数时会立即出现堆栈溢出。我应该读一个文本文件并做一些处理。有人能给我解释一下原因并提出解决办法吗C++ Don';我不知道为什么堆栈溢出,c++,memory,stack-overflow,C++,Memory,Stack Overflow,我不明白为什么当我进入主函数时会立即出现堆栈溢出。我应该读一个文本文件并做一些处理。有人能给我解释一下原因并提出解决办法吗 #include <iostream> #include <ctime> #include <cstdlib> #include <fstream> #include <iomanip> using namespace std; const int MAX=100; enum countrytype{S,F};
#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);*/
}
MAX被定义为100(它真的需要吗?),你有一堆字符数组MAX元素的长度,你甚至有一个2D数组,这是巨大的。因此,您的结构非常庞大,可能超过了最大堆栈大小——我认为在windows中是1024Kb MAX被定义为100(它真的需要吗?),你有一堆字符数组MAX元素的长度,你甚至有一个2D数组,这是巨大的。因此,您的结构非常庞大,可能超过了最大堆栈大小——我认为在windows中是1024Kb 简单地说,您的代码正在堆栈上分配其所有存储,并且分配的存储超过了允许的限制 看看你为什么超过了极限可能更有用
main()
的第一行是在堆栈上分配一个由100(最大=100)名学生组成的数组:
UOWstudent noofstudent[MAX];
学生有多大?您可以通过查看每个字段来了解这一点:
struct UOWstudent
{
countrytype ct; // enum. let's assume 4 bytes. (32-bit executable)
Student st; // ???
};
学生有多大
union Student
{
Local localstudent;
Foreign foreignstudent;
};
这是一个本地或外国的大小,所以让我们只看一个。我们需要对字符的大小做另一个假设。让我们假设1字节(8位字符):
main()的第一行尝试在堆栈上分配(10716+4)x100=1072000字节。我对编译器设置的char和int的大小做了最保守的假设,它们可能更高。如果堆栈限制确实是1兆字节(1048576字节),则此初始分配超出了限制
(UWWORK= =滑铁卢大学学生?)< P>简单地说,您的代码将所有的存储分配到堆栈上,并且您分配的余量超过允许的限制。
看看你为什么超过了极限可能更有用
main()
的第一行是在堆栈上分配一个由100(最大=100)名学生组成的数组:
UOWstudent noofstudent[MAX];
学生有多大?您可以通过查看每个字段来了解这一点:
struct UOWstudent
{
countrytype ct; // enum. let's assume 4 bytes. (32-bit executable)
Student st; // ???
};
学生有多大
union Student
{
Local localstudent;
Foreign foreignstudent;
};
这是一个本地或外国的大小,所以让我们只看一个。我们需要对字符的大小做另一个假设。让我们假设1字节(8位字符):
main()的第一行尝试在堆栈上分配(10716+4)x100=1072000字节。我对编译器设置的char和int的大小做了最保守的假设,它们可能更高。如果堆栈限制确实是1兆字节(1048576字节),则此初始分配超出了限制
(UWWORK= =滑铁卢大学学生)< /P>如果您使用<代码> STD::String ,您的代码将更易于管理。我在系统中不会获得堆栈溢出。您确定正在运行最新版本的可执行文件吗?你能给我们堆栈跟踪吗?在像我这样的64位平台上,你的UOWstudent
对象实例有10720字节宽。现在拿100个。Hmmm.…如果你停止在C++中使用原始数组,然后开始使用,生活会变得简单很多。因为我问了……代码>int main(){如果您使用std::string
,那么您的代码将更易于管理。我的系统上不会出现堆栈溢出。您确定您正在运行最新版本的可执行文件吗?您能给我们提供堆栈跟踪吗?您的UOWstudent
对象实例在像我这样的64位平台上有10720字节宽。Hmmm.,如果你在C++中停止使用原始数组,开始使用,那么生活会变得简单很多。因为我问了……代码> int(){cOUT + 1写得好。更大的是我的(因此我删除和支持这里)。+ 1写得好。更大的是我的(因此我删除和支持这里)。