[英]C++ Conway's Game of Life - Cygwin Segmentation Fault (Core Dumped)
我是這個論壇的新手,但我確實可以使用一些幫助。 我的任務是用C ++編程Conway的《生命游戲》並在Cygwin中編譯(帶有makefile)。 我並沒有要求任何人為我或類似的人完成該程序,但是我完全停留在這一部分上...該程序的一個方面是允許用戶輸入文本文件作為地圖初始網格,而不是使用隨機生成的網格。 這是.txt文件格式的示例(數字和'X'僅是示例,文件可以是此格式的任何變體):
5 (rows)
6 (columns)
-----X
X--X--
------
-XX---
------
“ X”代表帶有活細菌的空間,“-”代表死空間。 盡管我的程序可以通過cygwin正常編譯,但是當我運行.exe時,出現了“段錯誤(核心轉儲)”錯誤。 到目前為止,我已經在Google上進行了廣泛的搜索,但是我發現此錯誤通常是它所關心的程序的特有錯誤,因此其他解決方案對我沒有太大幫助。 我不想用大量代碼向你們發送垃圾郵件,所以現在我只包括我的loadFile函數。 如果您需要更多代碼幫助,請告訴我,我會立即發布。 這是我到目前為止在loadFile函數中所擁有的:
void GamePlay::loadFile(int r, int c, char** newBoard){
int i;
//int r;
//int c;
//char** newBoard;
int len;
int k;
do{
r = 0;
c = 0;
string filePath;
cout << "Please enter a file path" << endl;
cin >> filePath;
DIR* path = opendir(filePath.c_str());
string fpath = filePath.c_str();
dirent* openIn = readdir(path);
string line;
string ext = ".txt";
i = 0;
while(openIn && i != -1){
if(openIn->d_type == DT_REG){
string fileName = openIn->d_name;
string filePath = fpath + fileName;
int begExt = fileName.find_last_of(".");
int endExt = fileName.find_last_of("t");
string extension = fileName.substr(begExt, endExt);
ifstream in(filePath.c_str());
k = 0;
if(in && ext == extension){
getline(in,line);
istringstream(line) >> r;
getline(in,line);
istringstream(line) >> c;
newBoard = new char*[r]; //create multi-d array
for(int a = 0; a < r; ++a){
newBoard[a] = new char[c];
}
while(in.good() && i != -1){
if(k <= r){
if(len == c){
for(int g = 0; g < r; ++g){
getline(in,line);
len = line.size();
char* arr = new char[len];
memcpy(arr,line.c_str(),len);
for(int h = 0; h < c; ++h){
newBoard[g][h] = arr[h];
}
}
}
/*if(len == c){
//newBoard = len*sizeof(char*);
for(int a = 0; a < len; ++a){
newBoard[a] = r;
memcpy(newBoard[a], line, r);
}
}*/
else{
cout << "Your column number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
}
else{
cout << "Your row number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
k++;
}
}
else{
cout << "File invalid. Please try again with a new file." << endl;
i = -1;
}
return;
}
else{
cout << "Invalid file path. Please try again with a new file." << endl;
i = -1;
}
}
}while(i = -1);
}
我也嘗試過使用GDB進行調試,並找出問題所在,但這已經超出了我的理解范圍。 可悲的是,我習慣使用的只是GUI,例如Visual Studio和Eclipse。 任何幫助或建議,將不勝感激。 謝謝!!!
使用GDB非常簡單,
gdb [path-to-your-executable]
run
[wait for it to crash]
backtrace
這將告訴您崩潰發生的位置。 您還可以使用以下方式插入斷點
break file:line
停止時使用
print [some expression]
要查找有關當前堆棧幀(變量及其所有內容)的信息,然后“繼續”以移至下一個斷點。 如有必要,您還可以使用進行遠程調試
gdb [path-to-your-executable] [process ID]
如果您的程序在終端屏幕上輸出了很多信息,那會很好(因為否則它將與GDB的輸出混淆)。
如果您最初的興趣只是查找問題在代碼中的位置,則不一定需要調試器。 我從來沒有使用過,通常會設法從程序中刪除錯誤。 您可以設置標志並使用cout語句來確定程序在哪里出現問題。 當我在高中和大學中使用C ++時,我做了廣泛的工作。
我在下面的代碼中輸入了一些cout語句,以便您知道我在說什么。 您還沒有嘗試過,對嗎? 顯然,您運行程序並查看生成了哪些標志輸出。 如果出現了一個問題,而后來卻沒有出現,則說明您縮小了哪一行代碼使程序混亂。
void GamePlay::loadFile(int r, int c, char** newBoard){
int i;
//int r;
//int c;
//char** newBoard;
int len;
int k;
cout << "flag1";
do{
r = 0;
c = 0;
string filePath;
cout << "Please enter a file path" << endl;
cin >> filePath;
cout << "flag2";
DIR* path = opendir(filePath.c_str());
string fpath = filePath.c_str();
dirent* openIn = readdir(path);
string line;
string ext = ".txt";
i = 0;
cout << "flag3";
while(openIn && i != -1){
if(openIn->d_type == DT_REG){
string fileName = openIn->d_name;
string filePath = fpath + fileName;
int begExt = fileName.find_last_of(".");
int endExt = fileName.find_last_of("t");
string extension = fileName.substr(begExt, endExt);
cout << "flag4";
ifstream in(filePath.c_str());
k = 0;
if(in && ext == extension){
getline(in,line);
istringstream(line) >> r;
getline(in,line);
istringstream(line) >> c;
newBoard = new char*[r]; //create multi-d array
for(int a = 0; a < r; ++a){
newBoard[a] = new char[c];
}
cout << "flag5";
while(in.good() && i != -1){
if(k <= r){
if(len == c){
for(int g = 0; g < r; ++g){
getline(in,line);
len = line.size();
char* arr = new char[len];
memcpy(arr,line.c_str(),len);
for(int h = 0; h < c; ++h){
newBoard[g][h] = arr[h];
}
}
}
/*if(len == c){
//newBoard = len*sizeof(char*);
for(int a = 0; a < len; ++a){
newBoard[a] = r;
memcpy(newBoard[a], line, r);
}
}*/
else{
cout << "Your column number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
}
else{
cout << "Your row number does not match your grid." << endl;
cout << "Please try again with a new file." << endl;
i = -1;
}
k++;
}
}
else{
cout << "File invalid. Please try again with a new file." << endl;
i = -1;
}
return;
}
else{
cout << "Invalid file path. Please try again with a new file." << endl;
i = -1;
}
}
cout << "flag6";
}while(i = -1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.