Search code examples
arrayscmemorydynamicallocation

Why calling function second time is increasing column count by 5 times?


I am new to C coding and don't know much about dynamic allocation.I was working on an assignment.I am developing snake game in c.Below is the implementation of my code:

typedef struct snake_t {   
    unsigned int tail_row;
    unsigned int tail_col;
    unsigned int head_row;
    unsigned int head_col;            
    bool live;
} snake_t;
                
typedef struct game_state_t {
    unsigned int num_rows;
    char** board;
    unsigned int num_snakes;
    snake_t* snakes;
} game_state_t;
                
game_state_t* create_default_state() {
    // TODO: Implement this function.
    game_state_t *defaultState = (game_state_t*)malloc(sizeof(game_state_t));
    defaultState->num_rows = 18;
    defaultState->board = malloc(18 * sizeof(char *));
    
    for (int i = 0; i < 18; i++) {
        defaultState->board[i] = malloc(20 * sizeof(char));
    }
                  
    for (int i = 0; i < 18; i++) {
        for (int j = 0; j < 20; j++) {
            if (i == 0 || j == 0 || i == 18 - 1 || j == 20 - 1) {
                defaultState->board[i][j] = '#';
            }
            else {
                defaultState->board[i][j]=' ';
            }
        }
    }
                  
    defaultState->num_snakes = 1;
    defaultState->snakes = (snake_t*)malloc(sizeof(snake_t));
    defaultState->board[2][9] = '*';
    defaultState->snakes->tail_row = 2 ;
    defaultState->snakes->tail_col = 2 ;
    defaultState->snakes->head_row = 2 ;
    defaultState->snakes->head_col = 4 ;
    defaultState->board[2][2] = 'd';
    defaultState->board[2][3] = '>';
    defaultState->board[2][4] = 'D';
    defaultState->snakes->live = true;

    return defaultState;
}
                
game_state_t* actual_state = create_default_state();
game_state_t* expected_state = create_default_state();

actual_state is having correct number of rows and columns i.e. 18 and 20 respectively, but expected_state increase column by 5.When i print strlen of actual_state->board[0], it gives 20 but when i print strlen of expected_state->board[0] gives 25. Also when i tried to debug through gdb call function print on expected_state->board[0] it said repeated " 0x00577784 <repeated 20 times> , ' #\n#!' ". I cannot spot the bug. Why calling function second time increases column size by 5?


Solution

  • I have your answer, although it might be a bit too late!

    for (int i = 0; i < 18; i++)
    {
        for (int j = 0; j < 20; j++)
        {
            if (i == 0 || j == 0 || i == 18 - 1 || j == 20 - 1)
            {
                defaultState->board[i][j]='#';
            }
            else
            {
                defaultState->board[i][j]=' ';
            }
        }
    }
    

    So you have the above code section in your code. All you need to do to fix it is add defaultState->board[i][20] = '\0'; to the block like below:

    for (int i = 0; i < 18; i++)
    {
        for (int j = 0; j < 20; j++)
        {
            if (i == 0 || j == 0 || i == 18 - 1 || j == 20 - 1)
            {
                defaultState->board[i][j]='#';
            }
            else
            {
                defaultState->board[i][j]=' ';
            }
        }
        defaultState->board[i][20] = '\0';
    }
    

    This essentially follows from what was said above. Faced the same issue and this fixed it for me. This is a short answer please reply if you would like more explanation