2013-01-05 56 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中。

相關問題