Problem calling free() from another function [SOLVED]
Posted: Tue Oct 21, 2014 7:40 pm
I'm getting a segmentation fault when I try to free memory I allocate for a 2d array. The following causes a segmentation fault:
So the main game loop calls new_game(grid, &score, &goal), loops until the user quits (I haven't coded any of the actual game stuff yet) and then calls end_game(grid) when the user quits.
Now, when testing, this does NOT cause a segmentation fault:
My question: why do I get an error when I pass the Grid** 2d array to an external function and free, but do not get an error when I deallocate in the same location that I allocated the memory from? Am I not passing the 2d array "pointer pointer" properly?
If any of the above does make sense, please ask and I will clarify.
/* This function is the main game loop */ void game_loop() { /* Store the user input for logic */ Input input = NONE; /* Create game variables */ Grid** grid; int score; int goal; /* Initialize a new game */ new_game(grid, &score, &goal); while(true) { /* Get user input */ input = get_input(); if (input == QUIT) { end_game(grid); break; } } /* Go to the score loop */ score_loop(); } /* This function allocates a grid for a new game and initializes game data Param: grid Param: score Param: goal */ void new_game(Grid** grid , int* score, int* goal) { grid = malloc(GRID_SIZE * GRID_SIZE * sizeof(Grid)); int i; for (i = 0; i < GRID_SIZE; i++) { grid = malloc(GRID_SIZE * sizeof(Grid)); } /* Test load some data into this 2d array */ int r; int c; for(r = 0; r < GRID_SIZE; r++) { for (c = 0; c < GRID_SIZE; c++) { grid[r][c] = ONE; printf("%d ", grid[r][c]); } printf("\n"); } *score = 0; *goal = 15; } /* This function frees memory from a game Param: grid */ void end_game(Grid** grid) { int i; for (i = 0; i < GRID_SIZE; i++) { free(grid); } free(grid); }
So the main game loop calls new_game(grid, &score, &goal), loops until the user quits (I haven't coded any of the actual game stuff yet) and then calls end_game(grid) when the user quits.
Now, when testing, this does NOT cause a segmentation fault:
/* This function allocates a grid for a new game and initializes game data Param: grid Param: score Param: goal */ void new_game(Grid** grid , int* score, int* goal) { grid = malloc(GRID_SIZE * GRID_SIZE * sizeof(Grid)); int i; for (i = 0; i < GRID_SIZE; i++) { grid = malloc(GRID_SIZE * sizeof(Grid)); } /* Test load some data into this 2d array */ int r; int c; for(r = 0; r < GRID_SIZE; r++) { for (c = 0; c < GRID_SIZE; c++) { grid[r][c] = ONE; printf("%d ", grid[r][c]); } printf("\n"); } *score = 0; *goal = 15; /* Test free the 2d array allocated in this function */ for (i = 0; i < GRID_SIZE; i++) { free(grid); } free(grid); }
My question: why do I get an error when I pass the Grid** 2d array to an external function and free, but do not get an error when I deallocate in the same location that I allocated the memory from? Am I not passing the 2d array "pointer pointer" properly?
If any of the above does make sense, please ask and I will clarify.