Fix memory warnings/errors/leaks per valgrind

This commit is contained in:
2023-03-30 12:06:13 -04:00
parent 2f795be626
commit 1a1f5acb17
3 changed files with 57 additions and 27 deletions
+19 -3
View File
@@ -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
* 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->row = row;
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];
}
int print_boardcell(struct BoardCell *c) {
printf("%3s | (%d,%d)\n", c->letters, c->row, c->col);
return 0;
}
int print_board(Board *board) {
char *row_delim = malloc((board->width * 4 + 1) * sizeof(char));
row_delim[0] = '+';
uint8_t row_len = (board->width * 4 + 1) * sizeof(char) + 1;
char *row_delim = malloc(row_len);
snprintf(row_delim, 2, "%c", '+');
for (uint8_t i = 0; i < board->width; i++) {
strcat(row_delim, "----");
}
@@ -91,6 +97,16 @@ int print_board(Board *board) {
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) {
// // 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);
+8 -4
View File
@@ -25,13 +25,17 @@ int main(int argc, char **argv) {
print_board(&board);
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);
print_wordnode(root);
add_child_node(root, cells[5], false);
//add_child_node(root->children[0], cells[6], false);
add_child_node(&root, cells[5], false);
add_child_node(&root, cells[4], false);
print_wordnode(root);
print_wordnode_path(root->children[0]);
free_wordnode_children(root);
free(root);
for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
free(cells[i]);
}
free(cells);
}
+30 -20
View File
@@ -19,8 +19,26 @@ struct WordTree {
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);
return n;
}
void *print_wordnode_path(struct WordNode *n) {
@@ -38,27 +56,19 @@ void *print_wordnode_path(struct WordNode *n) {
return node;
}
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;
struct WordNode *add_child_node(struct WordNode **parent, struct BoardCell *cell, bool is_word) {
*parent = realloc(*parent, sizeof(struct WordNode) + sizeof(struct WordNode) * ((*parent)->child_cnt + 1));
(*parent)->children[(*parent)->child_cnt] = malloc(sizeof(struct WordNode));
init_wordnode((*parent)->children[(*parent)->child_cnt], is_word, cell, *parent, NULL, 0);
(*parent)->child_cnt++;
return (*parent)->children[(*parent)->child_cnt];
}
struct WordNode *add_child_node(struct WordNode *parent, struct BoardCell *cell, bool is_word) {
parent = realloc(parent, sizeof(struct WordNode) + sizeof(struct WordNode*));
parent->children[parent->child_cnt] = malloc(sizeof(struct WordNode));
init_wordnode(parent->children[parent->child_cnt], is_word, cell, parent, NULL, 0);
parent->child_cnt++;
return parent->children[parent->child_cnt];
void free_wordnode_children(struct WordNode *n) {
// TODO: recursively free wordnode children
for (uint8_t i = 0; i < n->child_cnt; i++) {
free(n->children[i]);
}
}
void *insert_word(struct WordTree *tree, char *word) {