destroyGrid() frees grid contents and itself.

This commit is contained in:
Sheldon Lee 2020-06-27 01:20:49 +01:00
parent 00ffe88289
commit ee5c58ad81
2 changed files with 12 additions and 1 deletions

10
grid.c
View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <ncurses.h>
#include "grid.h" #include "grid.h"
// locally used declaration // locally used declaration
@ -78,6 +79,13 @@ void drawGrid(Grid* grid)
} }
} }
void destroyGrid(Grid* grid)
{
free(grid->state);
free(grid->next_state);
free(grid);
printf("Grid destroyed\n");
}
// locally used // locally used
// check if cell's next state is alive // check if cell's next state is alive
static bool isAliveNext(Grid* grid, int x, int y) static bool isAliveNext(Grid* grid, int x, int y)

3
grid.h
View File

@ -10,6 +10,7 @@ typedef struct Grid {
bool* next_state; bool* next_state;
} Grid; } Grid;
// inits grid and returns pointer to grid. Remember to use Destroy Grid to deallocate
Grid* initGrid(unsigned int width, unsigned int height); Grid* initGrid(unsigned int width, unsigned int height);
void randomizeGrid(Grid* grid); void randomizeGrid(Grid* grid);
@ -26,5 +27,7 @@ void setPixel(Grid* grid, int x, int y, bool on);
void drawGrid(Grid* grid); void drawGrid(Grid* grid);
void destroyGrid(Grid* grid);
#endif #endif