C字符串指针初始化自己然后取消初始化自己?

时间:2017-07-18 11:16:30

标签: c

这是我正在撰写的康威生命游戏计划的摘录。在这部分中,我试图让程序读取一个文件,该文件指定在游戏开始时填充哪些单元格(即种子)。

我得到一个奇怪的错误。在read_line函数中,程序在line[i++] = ch语句中崩溃。当我调试程序时,我发现line - 指针在崩溃时为NULL。很公平,我认为,我应该初始化line。但这是(对我来说)奇怪的部分:

read_line函数已成功执行两次,并从种子文件中获取前两行(4\n3 6\n)。当我查看调试器中的执行时,我发现该行确实在read_line的前两次执行中保留了一个值。这怎么可能? line如何在没有初始化它的情况下进行初始化,然后突然不再被初始化了?

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

#define MAX_COORDINATE_SIZE 50
#define MAX_FILENAME_SIZE 20
#define MAX_GENERATIONS 10
#define MAX_REPETITION_PERIOD 4

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

struct cell{
    int pop;    //Populated
    int age;

};

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);

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

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

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

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

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

    FILE *fp = fopen(argv[1], "r");
    strncat(gens_string, argv[2], MAX_GENERATIONS);
    gens = atoi(gens_string);

    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); // get the first line of the file (number of popuated cells)

    *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++){ // for each line in the file (each populated cell)
        read_line(fp, line, MAX_COORDINATE_SIZE);
        coords[i] = read_coords(line); //Put coordinates in coords
        line = '\0';
    }

    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
    struct coord c;
    char *x;
    char *y;
    x = malloc(sizeof(char)*MAX_COORDINATE_SIZE);
    y = malloc(sizeof(char)*MAX_COORDINATE_SIZE);

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

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

    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)[i][j] = new_cell(i, j, 0, 0);
        }
    }
}

void print_board(struct cell **board, int nrows, int ncols){
    printf("--------------------\n");
    for(int i = 0; i<nrows; i++){
        for(int j = 0; j<ncols; j++){
            if(board[i][j].pop == 1){
                printf("%d ", board[i][j].age);
            }else if(board[i][j].pop == 0){
                printf("  ");
            }else{
                printf("\n\nERROR!");
                exit(0);
            }
        }
        printf("\n");
    }
    printf("--------------------");
    printf("\n");
}


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 cells specified 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){
    for(int i = 0; i < *n; i++){
        (*board)[coords[i].x][coords[i].y].pop = 1; //populate the cell
    }
}

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

种子文件:

4
3 6
4 6
5 6
5 7

编辑: 错误消息:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

我将补充说,如果我在line = malloc(sizeof(char)*MAX_COORDINATE_SIZE+1)中声明行后添加一行read_init,我仍然会收到相同的错误。

1 个答案:

答案 0 :(得分:3)

在read_init()中:

4 sec 771 ms