mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Fix memory warnings/errors/leaks per valgrind
This commit is contained in:
+19
-3
@@ -57,7 +57,7 @@ struct BoardCell *create_board_cell(char *letters, uint8_t row, uint8_t col) {
|
|||||||
/* BoardCells are created with 8 adjacent cells since that is the minimum required
|
/* BoardCells are created with 8 adjacent cells since that is the minimum required
|
||||||
* for centrally placed (not on sides or corners) cells on a board.
|
* for centrally placed (not on sides or corners) cells on a board.
|
||||||
*/
|
*/
|
||||||
struct BoardCell *cell = malloc(sizeof(struct BoardCell*));
|
struct BoardCell *cell = malloc(sizeof(struct BoardCell));
|
||||||
cell->letters = letters;
|
cell->letters = letters;
|
||||||
cell->row = row;
|
cell->row = row;
|
||||||
cell->col = col;
|
cell->col = col;
|
||||||
@@ -69,9 +69,15 @@ struct BoardCell *get_cell_at(Board *board, Vec2 pos) {
|
|||||||
return board->cells[pos.row * pos.col + pos.col];
|
return board->cells[pos.row * pos.col + pos.col];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int print_boardcell(struct BoardCell *c) {
|
||||||
|
printf("%3s | (%d,%d)\n", c->letters, c->row, c->col);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int print_board(Board *board) {
|
int print_board(Board *board) {
|
||||||
char *row_delim = malloc((board->width * 4 + 1) * sizeof(char));
|
uint8_t row_len = (board->width * 4 + 1) * sizeof(char) + 1;
|
||||||
row_delim[0] = '+';
|
char *row_delim = malloc(row_len);
|
||||||
|
snprintf(row_delim, 2, "%c", '+');
|
||||||
for (uint8_t i = 0; i < board->width; i++) {
|
for (uint8_t i = 0; i < board->width; i++) {
|
||||||
strcat(row_delim, "----");
|
strcat(row_delim, "----");
|
||||||
}
|
}
|
||||||
@@ -91,6 +97,16 @@ int print_board(Board *board) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_board(Board *b) {
|
||||||
|
if (b != NULL) {
|
||||||
|
for (uint8_t i = 0; i < b->width * b->height; i++) {
|
||||||
|
free(b->cells[i]);
|
||||||
|
b->cells[i] = NULL;
|
||||||
|
}
|
||||||
|
free(b->cells);
|
||||||
|
free(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
//Vec2[] get_adjacent_indexes(uint8_t board_size, Vec2 pos) {
|
//Vec2[] get_adjacent_indexes(uint8_t board_size, Vec2 pos) {
|
||||||
// // Calculate total possible number of adjacent cells
|
// // Calculate total possible number of adjacent cells
|
||||||
// uint16_t = total_adj_cell_cnt = ((board-size-2) * (board-size-2) * 8) + ((board-size-2) * 4 * 5) + (4 * 3);
|
// uint16_t = total_adj_cell_cnt = ((board-size-2) * (board-size-2) * 8) + ((board-size-2) * 4 * 5) + (4 * 3);
|
||||||
|
|||||||
@@ -25,13 +25,17 @@ int main(int argc, char **argv) {
|
|||||||
print_board(&board);
|
print_board(&board);
|
||||||
free(cell_letters);
|
free(cell_letters);
|
||||||
|
|
||||||
struct WordNode *root = malloc(sizeof(struct WordNode));
|
struct WordNode *root = malloc(sizeof(struct WordNode) + sizeof(struct WordNode*));
|
||||||
init_wordnode(root, false, cells[0], NULL, NULL, 0);
|
init_wordnode(root, false, cells[0], NULL, NULL, 0);
|
||||||
print_wordnode(root);
|
add_child_node(&root, cells[5], false);
|
||||||
add_child_node(root, cells[5], false);
|
add_child_node(&root, cells[4], false);
|
||||||
//add_child_node(root->children[0], cells[6], false);
|
|
||||||
print_wordnode(root);
|
print_wordnode(root);
|
||||||
print_wordnode_path(root->children[0]);
|
print_wordnode_path(root->children[0]);
|
||||||
|
|
||||||
|
free_wordnode_children(root);
|
||||||
free(root);
|
free(root);
|
||||||
|
for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
|
||||||
|
free(cells[i]);
|
||||||
|
}
|
||||||
|
free(cells);
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-20
@@ -19,8 +19,26 @@ struct WordTree {
|
|||||||
struct WordNode *root;
|
struct WordNode *root;
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_wordnode(struct WordNode *n) {
|
void *init_wordnode(struct WordNode *n, bool is_word, struct BoardCell *cell,
|
||||||
|
struct WordNode *parent, struct WordNode *children[], uint8_t child_cnt) {
|
||||||
|
n->is_word = is_word;
|
||||||
|
n->cell = cell;
|
||||||
|
n->parent = parent;
|
||||||
|
if (children != NULL) {
|
||||||
|
for (uint8_t i = 0; i < child_cnt; i++) {
|
||||||
|
n->children[i] = children[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n->child_cnt = child_cnt;
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
void *print_wordnode(struct WordNode *n) {
|
||||||
|
if (n == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
printf("%3s | (%d,%d) | %3d\n", n->cell->letters, n->cell->row, n->cell->col, n->child_cnt);
|
printf("%3s | (%d,%d) | %3d\n", n->cell->letters, n->cell->row, n->cell->col, n->child_cnt);
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *print_wordnode_path(struct WordNode *n) {
|
void *print_wordnode_path(struct WordNode *n) {
|
||||||
@@ -38,27 +56,19 @@ void *print_wordnode_path(struct WordNode *n) {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *init_wordnode(struct WordNode *n, bool is_word, struct BoardCell *cell,
|
struct WordNode *add_child_node(struct WordNode **parent, struct BoardCell *cell, bool is_word) {
|
||||||
struct WordNode *parent, struct WordNode *children[], uint8_t child_cnt) {
|
*parent = realloc(*parent, sizeof(struct WordNode) + sizeof(struct WordNode) * ((*parent)->child_cnt + 1));
|
||||||
n->is_word = is_word;
|
(*parent)->children[(*parent)->child_cnt] = malloc(sizeof(struct WordNode));
|
||||||
n->cell = cell;
|
init_wordnode((*parent)->children[(*parent)->child_cnt], is_word, cell, *parent, NULL, 0);
|
||||||
n->parent = parent;
|
(*parent)->child_cnt++;
|
||||||
if (children != NULL) {
|
return (*parent)->children[(*parent)->child_cnt];
|
||||||
for (uint8_t i = 0; i < child_cnt; i++) {
|
|
||||||
n->children[i] = children[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n->child_cnt = child_cnt;
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WordNode *add_child_node(struct WordNode *parent, struct BoardCell *cell, bool is_word) {
|
void free_wordnode_children(struct WordNode *n) {
|
||||||
parent = realloc(parent, sizeof(struct WordNode) + sizeof(struct WordNode*));
|
// TODO: recursively free wordnode children
|
||||||
parent->children[parent->child_cnt] = malloc(sizeof(struct WordNode));
|
for (uint8_t i = 0; i < n->child_cnt; i++) {
|
||||||
init_wordnode(parent->children[parent->child_cnt], is_word, cell, parent, NULL, 0);
|
free(n->children[i]);
|
||||||
parent->child_cnt++;
|
}
|
||||||
return parent->children[parent->child_cnt];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *insert_word(struct WordTree *tree, char *word) {
|
void *insert_word(struct WordTree *tree, char *word) {
|
||||||
|
|||||||
Reference in New Issue
Block a user