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

Linux下C语言开发Robots游戏出现Segmentation fault (core dumped)如何解决

段错误出现原因
  • 零长度数组无可用内存:你全局定义的char playfield[0][0]是零长度数组,本身没有分配任何可读写的内存空间,后续对playfield[i][j]的赋值操作都是非法内存访问,这是触发段错误的核心原因。另外代码中playfield[x][y], "\0";是无效的逗号表达式,不会完成任何内存分配操作,完全没有作用。
  • 数组下标越界:placeObjects函数中你使用rand() % (x + 1)生成坐标,得到的数值范围是0x,但数组行下标的合法范围是0x-1,列下标同理,访问playfield[x][...]或playfield[...][y]时会超出数组边界,触发非法访问。
  • 函数未声明即调用:placeObjects函数定义在main函数之后,且没有提前声明函数原型,C语言会默认该函数返回int类型,和实际的void返回类型不匹配,会引发未定义行为。
  • 其他隐含问题:两次输入尺寸的提示均标注为Y轴,逻辑错误;rand()没有初始化随机种子,每次运行生成的对象位置完全一致;placeObjects循环条件为i <= amountOfObjs,会多生成1个目标对象。
修复方案
  1. 修复playfield的内存分配:因为你限制了x、y最大为75,最简单的方案是直接定义足够大的静态数组,把全局的char playfield[0][0];改为char playfield[76][76];即可。如果需要动态分配,可以使用malloc实现:
    // 全局定义改为
    char **playfield;
    // 输入完x、y之后添加分配代码
    playfield = malloc(sizeof(char*) * x);
    for(int i = 0; i < x; i++){
        playfield[i] = malloc(sizeof(char) * y);
    }
    
  2. 修复坐标越界问题:将placeObjects里的rand() % (x + 1)改为rand() % x,rand() % (y + 1)改为rand() % y,保证坐标落在合法范围内。
  3. 提前声明函数:在main函数之前添加声明void placeObjects(int amountOfObjs, char Obj);。
  4. 修复输入提示错误:将第一次输入尺寸的提示改为X轴相关描述。
  5. 初始化随机种子:在调用rand之前添加srand((unsigned int)time(NULL));,同时引入头文件#include <time.h>。
  6. 修正循环条件:将placeObjects里的循环条件改为i < amountOfObjs,避免多生成对象。
修复后完整参考代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int x; //行
int y; //列
int robots;
int walls;
char playfield[76][76]; // 直接定义足够大的静态数组

// 提前声明函数
void placeObjects(int amountOfObjs, char Obj);

int main(int argc, char *argv[]) {
    // 初始化随机种子
    srand((unsigned int)time(NULL));

    printf("Enter the size of the X-Axis! It should be bigger than 5 and lower than 75!\n");
    printf("It is recommended that you use odd numbers!\n");
    scanf("%i", &x);
    if(x > 75 || x < 5) {
        printf("Input out of range!\n");
        exit(0);
    }
    printf("Enter the size of the Y-Axis! It should be bigger than 5 and lower than 75!\n");
    printf("It is recommended that you use odd numbers!\n");
    scanf("%i", &y);
    if(y > 75 || y < 5) {
        printf("Input out of range!\n");
        exit(0);
    }
    printf("Enter the amount of walls!\n");
    scanf("%i", &walls);
    printf("Now enter the amount of robots!\n");
    scanf("%i", &robots);

    //初始化场地
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            playfield[i][j] = '-';
        }
    }

    playfield[x/2][y/2] = 'O'; //设置玩家出生点
    placeObjects(walls, '#');
    placeObjects(robots, 'R'); // 补充生成机器人的调用

    //打印场地
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            printf("%c ", playfield[i][j]);
        }
        printf("\n");
    }

    return 0;
}

//生成墙和机器人
void placeObjects(int amountOfObjs, char Obj) {
    for(int i = 0; i < amountOfObjs; i++) {
        int ObjX = rand() % x;
        int ObjY = rand() % y;
        if(playfield[ObjX][ObjY] == '-') { 
            playfield[ObjX][ObjY] = Obj;
        } else {
            i--;
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 02:09:03