gpt4 book ai didi

C - 段。指针数组错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:58 25 4
gpt4 key购买 nike

我正在用 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));

关于C - 段。指针数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48350800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com