我目前正在完成一项作业,该作业应该证明我对如何在2D数组中有效使用指针和指针算术的理解。以下功能的目的是访问和检查阵列中任何给定位置的周围位置。如果周围的位置超出范围,则将忽略该位置并且不对其进行检查。不幸的是,调用该函数会导致分段错误(内存核心转储)。
运行调试器并记录可在[此处] [1]找到的结果之后,我能够确定错误出在bottom = *boardArr.
上,我将底线设置为* boardArr的原始原因是到boardArr的起始地址的底部,这样我就可以横穿与之相邻的其他位置,以便可以使用指针算法检查它们,但实际情况并非如此。我也尝试设置bottom = nullptr
,但未向控制台返回任何内容。如果有人可以为我指出正确的方向,以解决我的问题,将不胜感激。
更新:规则说明可在下面找到:
SimBacteria规则
///模拟游戏仍然是视频游戏的流行类型,您已经决定通过设计SimBacteria来利用它们的成功。 //给定一个由单细胞生物随机分布的区域,玩家将能够观察到他们的培养物在多代细菌中都蓬勃发展(或死亡)。 //您的程序将驱动仿真并显示每一代,然后将最后一代记录到文件中。
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
bool inbounds( int &rows, int &cols, int i, int j);
void rules(char** boardArr, int cols, int rows, char** updateArr);
int update(char**boardArr);
char tmp;
int main()
{
char**boardBegin;//reset pointers
char**UboardBegin;//resets pointers
char**updateArr;
char**boardArr;
char*ptr;
int gen;
char check;
fstream inFile;//open file
int rows=0;
int cols =0;
string line;//read each char from file
cout <<"\t"<< "Bacteria Generation Simulation" << "\n\n";
inFile.open("simbac.txt", ios::in |ios::out);
//input validation
if (inFile.fail())
{
cout << "Unable to open file";
exit(1);
}
cout<<"Enter generation amount: ";
cin>>gen;
while(gen <= 0 || gen > 10) //input validation
{
//acount for char entry later
cout<<"Error, try again\n";
cin>>gen;
}
//cout<<"Before loop"<<endl;
//determine number of cols and rows
while (getline(inFile, line))
{
cols = line.length();
rows++;
}
//parsing file into boardArr
boardArr = new char*[rows];
updateArr = new char*[rows];
boardBegin = boardArr;
UboardBegin = updateArr;
for(int i = 0; i < rows; i++, boardArr++,updateArr++)
{
boardArr[i] = new char[cols];
updateArr[i] = new char[cols];
inFile.clear();//resets file read
inFile.seekg(0, ios::beg);//resets pos of file read
}
boardArr = boardBegin;//Resets pointer
updateArr= UboardBegin;//Resets pointer
//cout<<"Entering "<<endl;
inFile.seekg(0, ios::beg);
//Inserting stuff into 2d arry
for(int i = 0; i <= rows; i++) //rows
{
//cout<<"Inside inserting stuff into 2d arry"<<endl;
ptr = *boardArr;
//cout<< i<<endl;
for(int j = 0; j <= cols; j++) //cols
{
cout<<*ptr;
ptr++;//moves to the next address
}
cout<<endl;
}
//run until reaches gen amount
for(int i = 0 ; i <= gen; i++)
{
//cout<<"Entering "<< i<<endl;
rules(boardArr,cols, rows, updateArr);
}
return 0;
}
//move through string by parsing to insert each char into array element position
void rules(char** boardArr,int cols, int rows, char**updateArr)
{
char *current;
char *pos;
char *upos;
char n;
char* top = nullptr;
char* bottom = nullptr;
//cout<<"Before for loop"<<endl;
for(int i = 0; i < rows; i++, boardArr++,updateArr++) //rows
{
char ncount = 0;
if(i < rows -1)
{
boardArr++;
// bottom = *boardArr;
boardArr--;//resets
}
else
{
bottom = nullptr;
}
pos = *boardArr;//
//cout<<"Before for loop"<<endl;
for(int j = 0; j < cols; j++, pos++, upos++) //cols
{
//<<"Inside for loop"<<endl;
//n = *pos;
*upos = n;
if(j != 0)
{
pos--;
if(*pos = '*')
{
ncount++;
//moves to the right
}
pos++;
if(j != cols -1)
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos--;
}
}
else
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos++;
}
if(top != nullptr)
{
pos = j + top;//moves to the top right
if(*pos = '*')
{
ncount++;
}
pos++;
if(j != 0)
{
pos--;
if(*pos = '*')
{
ncount++;
//moves to the right
}
pos++;
if(j != cols -1)
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos--;
}
}
else
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos++;
}
}
if(bottom != nullptr)
{
pos = j + bottom;
if(*pos = '*')
{
ncount++;
}
pos++;
if(j != 0)
{
pos--;
if(*pos = '*')
{
ncount++;
//moves to the right
}
pos++;
if(j != cols -1)
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos--;
}
}
else
{
pos++;
if(*pos = '*')
{
ncount++;
}
pos++;
}
}
//If a cell contains an organism and has fewer than 2 neighbors, the organism dies of loneliness.
if(ncount < 2 || ncount > 3)
{
*upos = ' ';
}
else if(ncount == 3)
{
*upos = '*';
}
}
top = *boardArr;
}
}
/*
bool inbounds( int &rows, int &cols, int i, int j)
{
if();
}
int update(char**boardArr)
{
// boardArr = uboardArr;//copies content from old board into new board
//make changes to uboardArr here
//return UboardArr;
}
//open file print
*/