[Hi Stack Overflow社区,我目前正在使用struct在C语言中编写扫雷游戏。
我使用的结构看起来像这样:
typedef struct cell_s {
bool isFlagged;
bool isMine;
bool isRevealed;
//struct coordinates_s coordinates;
} cell;
typedef struct board_s {
uint8_t width;
uint8_t height;
struct cell_s *cells;
} board;
分配内存,我使用以下功能:
board *Minesweeper = initialize(10,10);
看起来像这样:
board *initialize(uint8_t height, uint8_t width){
printf("[*] Allocating memory\n");
uint8_t *game_memory = malloc(sizeof(board) + sizeof(cell) * width * height);
printf("[*] creating Game Instance\n");
board *Minesweeper = (board *)game_memory;
//Initing Board Parameters
printf("[*] setting parameters\n");
Minesweeper->cells = (cell *)game_memory + sizeof(board);
Minesweeper->width = width;
Minesweeper->height = height;
printf("[+] board width: %d\n",Minesweeper->width);
printf("[+] board height: %d\n",Minesweeper->height);
printf("[+] setting up cells");
memset(Minesweeper->cells, 0, sizeof(cell) * width * height);
return Minesweeper;
}
程序本身运行良好,但是在使用valgrind检查时,它报告许多错误(主要在单元的memset部分),在未分配的内存区域中进行写入/读取操作(稍后将粘贴valgrind输出)。] >
我无法发现错误。有人可以启发我,我在分配方面做错了吗?
您好,Stack Overflow社区,我目前正在使用C语言编写结构的minewsweeper游戏。我使用的结构看起来像这样:typedef struct cell_s {bool isFlagged; bool isMine;布尔...
Minesweeper->cells = (cell *)game_memory + sizeof(board);