尝试访问初始化数组时的EXC_BAD_ACCESS

时间:2017-07-17 16:11:23

标签: c

在这个相当冗长的程序中,我收到一个错误。我试图在较小的程序中隔离问题,但是我没有成功,问题在较小的程序中消失了。所以我们走了:

这是生活游戏的简单实现。它还没有完成。 我已用注释标记了错误发生的行。它位于populate_board()函数中,这是从底部开始的第二个函数。首先是错误消息:

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

当我尝试访问board数组的元素时,会发生这种情况。我以前没有初始化阵列然后将其编入索引时,我曾经遇到过这个错误。但是在这里,我已经在read_init()函数中清楚地初始化了数组。你们也应该知道这个错误是在第一次执行标记行时发生的。我不知道这里发生了什么。这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_COORDINATE_SIZE 50
#define MAX_FILENAME_SIZE 20

struct coord{ //Holds coordinates to a cell
    int x;
    int y;
};

struct cell{
    int pop;    //Populated

};

struct coord *read_init(FILE *fp, int *i);

static int read_line(FILE *fp, char *line, int max_length);

struct coord read_coords(char *line);

struct cell **create_board(int x, int y);

void populate_board(struct coord *coords, struct cell **board, int *n);

struct cell new_cell(int x, int y, int pop);

struct cell **start_game(FILE *fp, int nrows, int ncols);

void print_board(struct cell **board, int nrows, int ncols);


int main(int argc, const char * argv[]) {
    if(argc != 2){
        fprintf(stderr, "Usage: %s [<seed-file>]\n<seed-file> can me up to %d characters long", argv[0], MAX_FILENAME_SIZE);
        exit(1);
    }
    FILE *fp = fopen(argv[1], "r");

    int nrows = 10;
    int ncols = 10;

    struct cell **board= start_game(fp, nrows, ncols);
    print_board(board, nrows, ncols);
    return 0;
}

struct coord *read_init(FILE *fp, int *n){ //Takes in filename and returns list of coordinates to be populated
    char raw_n[100];
    struct coord *coords;
    char *line;

    read_line(fp, raw_n, 100);

    *n = atoi(raw_n);//make an int out of raw_n

    coords = malloc(sizeof(struct coord)*(*n)); //Allocate memory for each coord

    for(int i = 0; i<(*n); i++){
        read_line(fp, line, MAX_COORDINATE_SIZE);
        coords[i] = read_coords(line); //Put coordinates in coords
    }

    return coords; // return coordinates
}

static int read_line ( FILE *fp, char *line, int max_length)
{
    int i;
    char ch;
    /* initialize index to string character */
    i = 0;
    /* read to end of line, filling in characters in string up to its
     maximum length, and ignoring the rest, if any */
    for(;;)
    {
        /* read next character */
        ch = fgetc(fp);
        /* check for end of file error */
        if ( ch == EOF )

            return -1;
        /* check for end of line */
        if ( ch == '\n' )
        {
            /* terminate string and return */
            line[i] = '\0';
            return 0;
        }
        /* fill character in string if it is not already full*/
        if ( i < max_length )
            line[i++] = ch;
    }
    /* the program should never reach here */
    return -1;
}

struct coord read_coords(char *line){ // Returns coordinates read from char *line
    int i = 0;
    struct coord c;
    char *x;
    char *y;
    x = malloc(sizeof(char)*MAX_COORDINATE_SIZE);
    y = malloc(sizeof(char)*MAX_COORDINATE_SIZE);

    do{
        x[i] = line[i];
        i++;
    }while(line[i] != ' ');
    i++;
    do{
        y[i-2] = line[i];
        i++;
    }while(line[i] != '\0');

    c.x = atoi(x);
    c.y = atoi(y);

    return c;
}

void init_board(int nrows, int ncols, struct cell **board){

    board = malloc(nrows * sizeof(*board) + nrows * ncols * sizeof(**board));

    //Now set the address of each row or whatever stackoverflow says
    struct cell * const firstrow = board + nrows;
    for(int i = 0; i < nrows; i++)
    {
        board[i] = firstrow + i * ncols;
    }

    for(int i = 0; i < nrows; i++){ //fill the entire board with pieces
        for(int j = 0; j < ncols; j++){
            board[j][i] = new_cell(i, j, 0);
        }
    }
}

struct cell new_cell(int x, int y, int pop){ //Return new populated or non-populated cell with specified coordinates
    struct cell c;
    c.pop = pop;
    return c;
}

struct cell **start_game(FILE *fp, int nrows, int ncols){ //x,y are no of rows/columns, fn is filename
    int n; // n is the number of populated rows in the seed
    struct coord *coords = read_init(fp, &n); // get the list of coords to populate board with
    struct cell **board;

    init_board(nrows, ncols, board); // Set up the board

    populate_board(coords, board, &n); //populate the cells specified in the seed

    return board;
}

void populate_board(struct coord *coords, struct cell **board, int *n){
    int x;
    int y;
    for(int i = 0; i < *n; i++){
        printf("%d", x = coords[i].x);
        printf("%d", y = coords[i].y);
        printf("%d", board[x][y].pop); // HERE IS THE ERROR
       // board[coords[i].x][coords[i].y].pop = 1; //populate the cell
    }
}

void print_board(struct cell **board, int nrows, int ncols){
    for(int i = 0; i<nrows; i++){
        for(int j = 0; j<ncols; j++){
            if(board[i][j].pop == 1){
                printf("X");
            }else{
                printf("O");
            }
        }
        printf("\n");
    }
    printf("\n");
}

0 个答案:

没有答案