C语言 分段核心转储


| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |

我正在尝试制作一个看起来像这样的网格。目前,每当我尝试运行可执行文件时,我都会不断转储分段故障核心。编译器也没有显示任何错误。我也不确定如何在网格内打印数字。以下是我目前拥有的代码(只是整个作业的一部分)。任何帮助将不胜感激。

void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
/* TODO */
int i,j;
char grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];
for(i = 0; i < BOARD_HEIGHT; i++)
{
for(j = 0; j < BOARD_WIDTH; j++)
{
grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|';
printf("%c", grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2]);
printf("%s", EMPTY_OUTPUT);
}
printf("n");
}    
}

BOARD_HEIGHT和BOARD_WIDTH是在头文件中定义的变量。

#ifndef BOARD_H
#define BOARD_H
#include "helpers.h"
#include "player.h"
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
typedef enum cell
{
EMPTY,
BLOCKED,
PLAYER
} Cell;
#define EMPTY_OUTPUT " "
#define BLOCKED_OUTPUT "*"
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH];
Cell BOARD_2[BOARD_HEIGHT][BOARD_WIDTH];
typedef enum playerMove
{
PLAYER_MOVED,
CELL_BLOCKED,
OUTSIDE_BOUNDS
} PlayerMove;

void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH]);
void loadBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Cell boardToLoad[BOARD_HEIGHT][BOARD_WIDTH]);
Boolean placePlayer(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Position position);
PlayerMove movePlayerForward(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Player * player);
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player);
#endif

第二部分在头文件中。

完整的代码可以在这里找到 a1

您的错误位于数组末尾之外索引网格的行上:

grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|'; // Bug

此行写入本地定义的网格数组末尾之外,如下所示:

char grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];

请注意,grid[][] 中的有效索引从 0 到 BOARD_HEIGHT+1,从 0 到 BOARD_WIDTH+1。您的代码在两个维度上都可以访问。

这会导致堆栈损坏,因此当显示板函数返回时,它已损坏堆栈,并且您会出现分段错误。删除此行可以消除 seg 错误,但代码仍然需要正常工作才能正常工作。

您应该引用电路板数组,并使用索引 i 和 j,而不是硬编码常量。

这是一个工作版本,我索引到板数组中,将当前单元格值存储在 currentCell 中,然后根据 switch 语句显示它。给定一个充满零的空板,这将产生以下输出:

scott> gcc -g -ogeraldTest -O0 board.c
scott> geraldTest 
| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |

这是main()和printBoard的代码,都在board.c中。请注意,printBoard 是按值传递板的,但最好通过引用传递它。

#include <stdio.h>
#include "board.h"
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i,j;
// First display the header at the top.
printf("| |");
for(j=0; j<BOARD_WIDTH;j++)
printf("%d|",j);
printf("n");
for(i = 0; i < BOARD_HEIGHT; i++)
{
// Display each row number
printf("|%d|",i);
for(j = 0; j < BOARD_WIDTH; j++)
{
// Bug: grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2] = '|';
Cell currentCell = board[i][j];
switch(currentCell) {
case BLOCKED:
printf("%s",BLOCKED_OUTPUT);
break;
case PLAYER:
printf("P");
break;
default:
printf("%s",EMPTY_OUTPUT);
break;
}
printf("|");
// This code is wrong
// printf("%c|", grid[BOARD_HEIGHT + 2][BOARD_WIDTH + 2]);
// printf("%s", EMPTY_OUTPUT);
}
printf("n");
}
}
int main(int argc, char ** argv)
{
Cell myBoard[BOARD_HEIGHT][BOARD_WIDTH] = {0};
Player bob = 0;
displayBoard(myBoard, &bob);
}

我拿了你的 board.h 并定义了缺少的类型,以便让它编译。您可以按原样使用自己的标头。

#ifndef BOARD_H
#define BOARD_H
// #include "helpers.h"
// #include "player.h"
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
typedef int  Boolean; // Added
typedef int  Player;  // Added
typedef struct {      // Added
int row;
int col;
} Position;
typedef enum cell
{
EMPTY,
BLOCKED,
PLAYER
} Cell;
#define EMPTY_OUTPUT " "
#define BLOCKED_OUTPUT "*"
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH];
Cell BOARD_2[BOARD_HEIGHT][BOARD_WIDTH];
typedef enum playerMove
{
PLAYER_MOVED,
CELL_BLOCKED,
OUTSIDE_BOUNDS
} PlayerMove;

void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH]);
void loadBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Cell boardToLoad[BOARD_HEIGHT][BOARD_WIDTH]);
Boolean placePlayer(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Position position);
PlayerMove movePlayerForward(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Player * player);
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player);
#endif

相关内容

  • 没有找到相关文章