2013-01-05 57 views
1

我不明白为什么我进入主函数时立即得到堆栈溢出。我应该从文本文件中读取并做一些处理。有人可以向我解释原因并提出解决方法吗?不知道为什么我得到一个堆栈溢出

#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 
+1

您的代码将是这样更易于管理的,如果你用'的std :: string'。 – chris

+0

我没有在我的系统上发生堆栈溢出。你确定你正在运行最新版本的可执行文件吗?你能给我们堆栈跟踪吗? – templatetypedef

+0

@chris我无法使用字符串类 – Computernerd

回答

8

简单地说,您的代码正在分配堆栈上的所有存储,并且您分配的数量超过了允许的限制。

看看你为什么超越极限可能更有用。

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 

所以主要的是第一行()尝试分配(10716 + 4)×堆100 = 1072000字节。对于编译器设置,我对char和int的大小做了最保守的假设,它们可能会更高。如果堆栈限制确实是一兆字节(1,048,576字节),那么这个初始分配超出限制。

You can use C's sizeof operator to get information about the actual size of your types. Refer to this stackoverflow answer, discussing allocating arrays on the heap, instead of the stack, which is a good step towards solving your problem. (滑铁卢UOWstudent ==大学的学生吗?)

+0

+1写得很好。相当多 - 所以我的矿(因此我的删除和支持在这里)。 – WhozCraig

+1

所以我*不是唯一一个想到“滑铁卢大学学生”的人?哇... – chris

1

MAX定义为100(它是否真的需要?),你有一堆长度的字符数组MAX元素,你甚至有一二维数组,这是巨大的。所以你的结构是巨大的,可能会超过最大堆栈大小 - 我认为它的1024Kb在Windows中。

相关问题