From ee5c58ad8148601f9716d262e6b18b99e4c9842d Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Sat, 27 Jun 2020 01:20:49 +0100 Subject: [PATCH] destroyGrid() frees grid contents and itself. --- grid.c | 10 +++++++++- grid.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/grid.c b/grid.c index a7cc0f9..ad8c8fc 100644 --- a/grid.c +++ b/grid.c @@ -1,6 +1,7 @@ +#include +#include #include #include -#include #include "grid.h" // 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 // check if cell's next state is alive static bool isAliveNext(Grid* grid, int x, int y) diff --git a/grid.h b/grid.h index fa18e09..5990206 100644 --- a/grid.h +++ b/grid.h @@ -10,6 +10,7 @@ typedef struct Grid { bool* next_state; } Grid; +// inits grid and returns pointer to grid. Remember to use Destroy Grid to deallocate Grid* initGrid(unsigned int width, unsigned int height); void randomizeGrid(Grid* grid); @@ -26,5 +27,7 @@ void setPixel(Grid* grid, int x, int y, bool on); void drawGrid(Grid* grid); +void destroyGrid(Grid* grid); + #endif