C - 赛格.指针数组中的错误



我正在用C语言做一个学校项目,我需要做的是创建一个2D代理网格,其中一些是人类,另一些是僵尸。人类逃离僵尸,僵尸试图感染他们。

游戏是回合制的,每个回合的移动顺序都是随机的。为了做到这一点,我创建了一个指针数组来容纳所有代理,这样我就可以在每回合之前洗牌数组,这样,我就可以通过循环浏览代理数组来获得随机代理移动顺序。

问题是,当我创建该指针数组时,我遇到了分段错误,我不明白为什么。分段错误发生在此处:

agents_shuffle[c] = agent_new(type, playable, id, x, y);

感谢您的帮助!

以下是程序主要部分的代码:

/* Array to save agent pointers for shuffle*/
AGENT **agents_shuffle = NULL;
AGENT_TYPE type = 0;
unsigned char playable = 0;
unsigned short id = 0;
int c = 0;
for (int x = 0; x < WORLD_X; x++) {
    for (int y = 0; y < WORLD_Y; y++) {
        if (agent_grid[x][y].type != None) {
            type = agent_grid[x][y].type;
            playable = agent_grid[x][y].playable;
            id = agent_grid[x][y].id;
            agents_shuffle[c] = agent_new(type, playable, id, x, y);
            c++;
        }
    }
}

下面是 agent.c 文件:

#include "agent.h"
AGENT *agent_new(AGENT_TYPE type, unsigned char playable, 
    short id, int x, int y) {
    AGENT *a = (AGENT *) malloc(sizeof(AGENT));
    a->type = type;
    a->playable = playable;
    a->id = id;
    a->x = x;
    a->y = y;
    return a;
}
void agent_destroy(AGENT *a) {
    free(a);
}

结构剂:

/**
 * Structure defining agent properties.
 * */
typedef struct {
    AGENT_TYPE type;        /**< Agent type.              */
    unsigned char playable; /**< Is agent playable?       */
    unsigned short id;      /**< Agent ID.                */
    int x;                  /**< Agent position on x axis */
    int y;                  /**< Agent position on y axis */
} AGENT;

下面是定义的代理网格:

/** Horizontal world size. */
#define WORLD_X 10
/** Vertical world size. */
#define WORLD_Y 10
/* A by-dimensional array of agents, representing agents in a grid. */
AGENT agent_grid[WORLD_X][WORLD_Y];
/* Number of agents created so far. */
unsigned int nagents = 0;
/* Initialize random number generator. */
srand(time(NULL));
/* **************************************************************** */
/* Cycle through all cells in grid and randomly place agents in it. */
/* **************************************************************** */
for (int i = 0; i < WORLD_X; ++i) {
    for (int j = 0; j < WORLD_Y; ++j) {
        /* Possible agent in grid. By default we assume there is none. */
        AGENT ag = {None, 0, 0, 0, 0};
        /* Obtain a probability between 0 and 99. */
        unsigned char probability = rand() % 100;
        /* There is 10% probability of creating an agent. */
        if (probability < 10) {
            /* If we got here, an agent will be placed at (i,j). */
            /* Randomly define agent type. */
            ag.type = (rand() % 2 == 0) ? Human : Zombie;
            /* Give 10% probablity of agent being playable by user. */
            ag.playable = (rand() % 10 == 0);
            /* Assign agent ID and then increment number of agents so
               far. */
            ag.id = nagents++;
            /* Agent Coordinates */
            ag.x = i;
            ag.y = j;
        }
        /* Assign possible agent to grid at (i,j). */
        agent_grid[i][j] = ag;
    }
}
当您

执行 agents_suffle[c] 时,您正在取消引用 NULL 指针agents_shuffle

在这里,agents_shuffle被定义为指向AGENT的指针数组:

AGENT **agents_shuffle = NULL;

您的 agent_new 函数为agent分配空间并返回指向它的指针,并尝试将其存储在 agents_shuffle[c] 处的数组中。 可能导致段错误的一个问题是,从未为此数组分配空间。

循环所需的最大空间量是数组中的 WORLD_X*WORLD_Y 个元素。

尝试:

agents_shuffle = malloc((WORLD_X*WORLD_Y)*sizeof(*agents_shuffle));

你计划agent_shuffle是一个指针数组。
但是您将其初始化为指向指针的指针

AGENT **agents_shuffle = NULL;您需要将上一行
更改为 AGENT *agents_shuffle[WORLD_X*WORLD_Y];

相关内容

  • 没有找到相关文章

最新更新