我正在制作生命终端游戏,当滑翔机人物走出地图时,会出现段错误 11。你能告诉我这个段错误具体出现在哪里以及如何解决它吗?
我的 main.cpp 看起来像这样:
#include "src/Board.hpp"
int main(){
Board board(1);
while(true){
board.new_frame();
}
}
我的 Board.hpp 看起来像这样:
#pragma once
#include <iostream>
#include <chrono>
#include <thread>
#define square_size 20
#define yMax square_size
#define xMax square_size * 5 / 2
class Board
{
private:
bool cells[yMax][xMax] = {{0}};
int frame;
public:
Board(){
setup();
}
Board(int n){
setup();
}
void setup(){
cells[10][10] = 1;
cells[10][11] = 1;
cells[10][9] = 1;
cells[11][11] = 1;
cells[12][10] = 1;
frame = 1;
}
void screenClear(){
system("clear");
}
void wait(){
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(50ms);
}
int count_near(int y, int x){
int cnt = 0;
for (int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
if (j!=0 || i != 0){
if (y + i < yMax && y + i >= 0 && x+j < xMax && x+j >= 0){
if (cells[y+i][x+j] == 1){
cnt++;
}
}
}
}
}
return cnt;
}
void output(int y, int x){
if (cells[y][x] == 1){
std::cout<<'*';
}else{
std::cout<<'.';
}
}
void new_frame(){
std::vector<int> changes;
screenClear();
for(int i = 0; i < yMax; i++){
for (int j = 0; j < xMax; j++){
output(i, j);
int cnt = count_near(i, j);
if (cells[i][j] == 1){
if (cnt < 2){
changes.push_back(i);
changes.push_back(j);
changes.push_back(0);
}else if (cnt > 3){
changes.push_back(i);
changes.push_back(j);
changes.push_back(0);
}
}else{
if (cnt == 3){
changes.push_back(i);
changes.push_back(j);
changes.push_back(1);
}
}
}
std::cout<<'\n';
}
for (int i = 0; i < changes.size() - 2; i+=3){
if (changes[i+2] == 0){
cells[changes[i]][changes[i+1]] = 0;
}else{
cells[changes[i]][changes[i+1]] = 1;
}
}
std::cout<<frame<<std::endl;
frame++;
wait();
changes.clear();
}
};
我用谷歌搜索并发现 seg failure 11 意味着访问不允许空间的内存空间,但是我的 count_near 函数检查 y 是否在 0 到 yMax 的范围内,x 是否在 0 到 xMax 的范围内,它仍然输出段错误当滑翔机飞出地图时。
int count_near(int y, int x) {
int cnt = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (j != 0 || i != 0) {
int newY = (y + i + yMax) % yMax; // Wrap around for y
int newX = (x + j + xMax) % xMax; // Wrap around for x
if (cells[newY][newX] == 1) {
cnt++;
}
}
}
}
return cnt;
}