C-Seg。指针数组中的错误

C-Seg。指针数组中的错误,c,arrays,pointers,segmentation-fault,C,Arrays,Pointers,Segmentation Fault,我正在做一个C语言的学校项目,我需要做的是创建一个2D代理网格,其中一些是人类,其他是僵尸。人类逃离僵尸,僵尸试图感染他们 游戏以回合为基础,每个回合的移动顺序都是随机的。为了做到这一点,我创建了一个指针数组,它将容纳所有的代理,这样我可以在每个回合之前洗牌数组,通过循环代理数组,我得到随机代理移动顺序 问题是,当我创建指针数组时,我会出现分段错误,我不明白为什么。分段错误发生在这里: agents_shuffle[c] = agent_new(type, playable, id, x, y)

我正在做一个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++;
        }
    }
}
结构代理:

/**
 * 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;
/**
*定义代理属性的结构。
* */
类型定义结构{
代理类型;/**<代理类型*/
未签名字符可播放;/**<代理是否可播放*/
未签名的短id;/**<代理id*/
int x;/**
以下是定义的代理网格:

/** 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;

    }
}
/**水平世界大小*/
#定义世界10
/**垂直世界大小*/
#定义世界10
/*代理的按维数组,表示网格中的代理*/
代理网格[WORLD_X][WORLD_Y];
/*到目前为止创建的代理数*/
无符号整数=0;
/*初始化随机数生成器*/
srand(时间(空));
/* **************************************************************** */
/*在网格中的所有单元格中循环并随机放置代理*/
/* **************************************************************** */
for(int i=0;i
当您执行
代理权限[c]
时,您正在取消引用空指针
代理权限

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

AGENT **agents_shuffle = NULL;
您的
agent\u new
函数为
agent
分配空间,返回指向它的指针,并尝试将其存储到
agent\u shuffle[c]
的数组中。可能导致segfault的一个问题是从未分配此阵列的空间

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

尝试:


您计划
代理\u shuffle
成为指针数组。
但您将其初始化为指向指针的指针
AGENT**AGENT\u shuffle=NULL
您需要将上一行更改为

AGENT*AGENT\u shuffle[WORLD\u X*WORLD\u Y]

代理网格在哪里定义?您为此分配内存吗?您需要为
代理\u shuffle
分配内存。我看不出在你的代码中发生了什么。我看到的是,您已将其分配给NULL。首先,您没有指定代理网格的位置以及如何为其分配内存。请记住,2D数组必须首先为外部数组分配内存,然后在数组上循环,并为每个单元格分配内存,以包含另一个数组。您也没有指定seg故障发生的位置。您需要查看:和。感谢您的回复!解决了的。
AGENT **agents_shuffle = NULL;
agents_shuffle = malloc((WORLD_X*WORLD_Y)*sizeof(*agents_shuffle));