diff --git a/src/command.c b/src/command.c index 58e3b65..3388e8b 100644 --- a/src/command.c +++ b/src/command.c @@ -35,4 +35,24 @@ cmd_desc_t *get_desc(char name[]) return &cmd_desc[i]; return NULL; +} + +void command_free(command_t *cmd) +{ + if (cmd->arg_v != NULL) + free(cmd->arg_v); + + free(cmd); +} + +void command_arr_free(command_t **arr) +{ + int pos = 0; + while (arr[pos] != NULL) + { + command_free(arr[pos]); + pos++; + } + + free(arr); } \ No newline at end of file diff --git a/src/command.h b/src/command.h index 45493c5..c1b87a7 100644 --- a/src/command.h +++ b/src/command.h @@ -41,5 +41,7 @@ typedef struct command_t *command_new(cmd_code_t type, int args[]); cmd_desc_t *get_desc(char name[]); +void command_free(command_t *cmd); +void command_arr_free(command_t **arr); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 42d6145..77f8a0a 100644 --- a/src/main.c +++ b/src/main.c @@ -61,11 +61,16 @@ int main(int argc, char *argv[]) command_t **commands = read_bin_file(argv[2], res); if (commands == NULL) { + stack_free(stack); fprintf(stderr, "Couldn't read file\n"); return 1; } - return run(commands, stack); + int response = run(commands, stack); + + stack_free(stack); + command_arr_free(commands); + return response; } else { diff --git a/src/read_bin.c b/src/read_bin.c index f1d4091..d851e0c 100644 --- a/src/read_bin.c +++ b/src/read_bin.c @@ -31,5 +31,7 @@ command_t **read_bin_file(char src[], int *res) buffer[size] = NULL; + fclose(file); + return buffer; } diff --git a/src/stack.c b/src/stack.c index 5e5e568..dcc2e43 100644 --- a/src/stack.c +++ b/src/stack.c @@ -59,4 +59,10 @@ int stack_get(stack_t *stack, int pos, int *result) } return stack->mem[pos]; +} + +void stack_free(stack_t *stack) +{ + free(stack->mem); + free(stack); } \ No newline at end of file diff --git a/src/stack.h b/src/stack.h index e964460..cb99495 100644 --- a/src/stack.h +++ b/src/stack.h @@ -14,5 +14,6 @@ stack_t *stack_new(int size, int *result); int stack_push(stack_t *stack, int value, int *result); int stack_pop(stack_t *stack, int *result); int stack_get(stack_t *stack, int pos, int *result); +void stack_free(stack_t *stack); #endif \ No newline at end of file