You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

终端生命游戏滑翔机越界触发Segmentation Fault 11的排查与修复

问题描述

我正在开发一款终端版生命游戏,当滑翔机(glider)移出地图范围时,会触发Segmentation Fault 11错误。我已在count_near函数中添加了边界检查,但问题仍未解决。

代码片段

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>
#include <vector>  // 原代码遗漏该头文件,需补充

#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();
    }
};
错误定位与修复方案

错误原因

段错误并非来自count_near的边界检查,而是出现在new_frame函数处理changes向量的循环中:

for (int i = 0; i < changes.size() - 2; i+=3){

这个循环条件逻辑错误:

  • 当changes元素个数为3的整数倍时,changes.size() - 2会让循环多执行一次,访问向量越界位置;
  • 当changes元素个数少于3时,changes.size() - 2会变成负数,导致循环条件恒成立,直接触发越界访问。

滑翔机移出地图时,边界细胞计算会生成新的changes条目,此时循环越界访问的内容恰好对应cells数组外的内存,触发Segmentation Fault 11。

修复步骤

  1. 修正循环条件
    将处理changes的循环条件改为:
for (int i = 0; i < changes.size(); i += 3) {

确保只处理完整的三元组(每个修改由y、x、状态三个元素组成),彻底避免越界访问。

  1. 补充缺失的头文件
    原代码使用std::vector但未包含<vector>头文件,会导致未定义行为,需在Board.hpp顶部添加:
#include <vector>

验证

修复后,滑翔机移出地图时,changes向量的处理不会再出现越界,cells数组的访问始终在合法范围内,段错误将不再触发。

内容的提问来源于stack exchange,提问作者RegularGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:50:59