-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_grid.c
47 lines (44 loc) · 1022 Bytes
/
make_grid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "terrain.h"
/**
* allocate_grid - Allocate nrows x ncols of SDL_Point structs
* to store window coordinates of points to be rendered.
* @nrows: number of rows to malloc
* @ncols: number of columns to malloc
*
* Return: 2d array of SDL_Point structs, or NULL on failure
*/
SDL_Point **allocate_grid(size_t nrows, size_t ncols)
{
SDL_Point **grid;
size_t i;
grid = malloc(sizeof(SDL_Point *) * nrows);
if (!grid)
{
perror("Could not malloc space for SDL_Point rows");
return (NULL);
}
for (i = 0; i < nrows; i++)
{
grid[i] = calloc(ncols, sizeof(SDL_Point));
if (!(grid[i]))
{
perror("Could not malloc space for SDL_Point columns");
free_grid(grid, i);
return (NULL);
}
}
return grid;
}
/**
* free_grid - routine to free mallocated 2d array of SDL_Point structs
* @grid: pointer to array to free
* @i: number of rows to free
*/
void free_grid(SDL_Point **grid, size_t i)
{
if (grid == NULL || *grid == NULL)
return;
while (i)
free(grid[i--]);
free(grid);
}