Examples and fixing stuff

This commit is contained in:
Leonardo Vencovsky committed 2023-01-25 13:35:16 +01:00
1 parent a8c1619be6
commit bd338f2fe3
10 files changed
+42 -47

No files matched your search

+15 -20
View File
@@ -2,36 +2,31 @@ CC=gcc
CFLAGS=-Wall -Wextra -Werror -O3 CFLAGS=-Wall -Wextra -Werror -O3
INCLUDE=-I ../src INCLUDE=-I ../src
dfs: dfs.c ifndef FILE
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) main:
./$@.exe @echo Please define a FILE parameter to run. Examples:
@echo " dfs"
example: example.c @echo " example"
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) @echo " hanoi"
./$@.exe @echo " heapsort"
@echo " ice_cream"
hanoi: hanoi.c @echo " sieve"
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) else
./$@.exe $(FILE): $(FILE).c
heapsort: heapsort.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
sieve: sieve.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe ./$@.exe
endif
clean: clean:
rm *.exe rm *.exe -f
rm *.txt rm *.txt -f
memcheck: memcheck:
ifndef FILE ifndef FILE
$(error FILE parameter must be set first to check for leaks) $(error FILE parameter must be set first to check for leaks)
else else
$(CC) $(FILE) -o mem.exe $(CFLAGS) $(INCLUDE) -g $(CC) $(FILE).c -o mem.exe $(CFLAGS) $(INCLUDE) -g
valgrind --leak-check=yes ./mem.exe valgrind --leak-check=yes ./mem.exe
endif endif
+1 -1
View File
@@ -61,7 +61,7 @@ int main(void)
printf("Stack:\n"); printf("Stack:\n");
fprintf(stdout, "[ "); fprintf(stdout, "[ ");
struct Stack *stack = stk_new(100, &(struct StackFVal){ NULL }); struct Stack *stack = stk_new(100, &(struct StackFVal){ 0 });
struct BinaryTreeNode *node = tree->root; struct BinaryTreeNode *node = tree->root;
+4 -4
View File
@@ -11,7 +11,7 @@ struct
struct stack *from; struct stack *from;
struct stack *aux; struct stack *aux;
struct stack *to; struct stack *to;
} print_aux = { NULL }; } print_aux = { 0 };
void print_stacks() void print_stacks()
{ {
@@ -50,9 +50,9 @@ void hanoi(int n, struct stack *from, struct stack *aux, struct stack *to)
int main() int main()
{ {
struct stack from = stk_init(10, &(struct stack_fval){ NULL }); struct stack from = stk_init(10, &(struct stack_fval){ 0 });
struct stack to = stk_init(10, &(struct stack_fval){ NULL }); struct stack to = stk_init(10, &(struct stack_fval){ 0 });
struct stack aux = stk_init(10, &(struct stack_fval){ NULL }); struct stack aux = stk_init(10, &(struct stack_fval){ 0 });
for (int i = disks; i >= 1; i--) for (int i = disks; i >= 1; i--)
stk_push(&from, i); stk_push(&from, i);
+6 -6
View File
@@ -48,7 +48,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE
return NULL; return NULL;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return NULL; return NULL;
if (!f_key || !f_val) if (!f_key || !f_val)
@@ -488,7 +488,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
goto success; goto success;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * _map_->load) if (capacity >= (double)UINTMAX_MAX * _map_->load)
{ {
_map_->flag = CMC_FLAG_ERROR; _map_->flag = CMC_FLAG_ERROR;
return false; return false;
@@ -526,8 +526,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
if (!e1 || !e2) if (!e1 || !e2)
{ {
/* Prevent the map from freeing the data */ /* Prevent the map from freeing the data */
_new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 };
_new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
_map_->alloc->free(_new_map_->buffer); _map_->alloc->free(_new_map_->buffer);
_map_->alloc->free(_new_map_); _map_->alloc->free(_new_map_);
@@ -544,8 +544,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
if (_map_->count != _new_map_->count) if (_map_->count != _new_map_->count)
{ {
/* Prevent the map from freeing the data */ /* Prevent the map from freeing the data */
_new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 };
_new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
_map_->alloc->free(_new_map_->buffer); _map_->alloc->free(_new_map_->buffer);
_map_->alloc->free(_new_map_); _map_->alloc->free(_new_map_);
+1 -1
View File
@@ -48,7 +48,7 @@ struct SNAME CMC_(PFX, _init_custom)(size_t capacity, double load, struct CMC_DE
return _map_; return _map_;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return _map_; return _map_;
if (!f_key || !f_val) if (!f_key || !f_val)
+4 -4
View File
@@ -42,7 +42,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE
return NULL; return NULL;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return NULL; return NULL;
if (!f_key || !f_val) if (!f_key || !f_val)
@@ -448,7 +448,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
goto success; goto success;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * _map_->load) if (capacity >= (double)UINTMAX_MAX * _map_->load)
{ {
_map_->flag = CMC_FLAG_ERROR; _map_->flag = CMC_FLAG_ERROR;
return false; return false;
@@ -504,8 +504,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
_new_map_->capacity = tmp_c; _new_map_->capacity = tmp_c;
/* Prevent the map from freeing the data */ /* Prevent the map from freeing the data */
_new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 };
_new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
CMC_(PFX, _free)(_new_map_); CMC_(PFX, _free)(_new_map_);
+1 -1
View File
@@ -48,7 +48,7 @@ struct SNAME CMC_(PFX, _init_custom)(size_t capacity, double load, struct CMC_DE
return _map_; return _map_;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return _map_; return _map_;
if (!f_key || !f_val) if (!f_key || !f_val)
+4 -4
View File
@@ -44,7 +44,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE
return NULL; return NULL;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return NULL; return NULL;
if (!f_key || !f_val) if (!f_key || !f_val)
@@ -658,7 +658,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
goto success; goto success;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * _map_->load) if (capacity >= (double)UINTMAX_MAX * _map_->load)
{ {
_map_->flag = CMC_FLAG_ERROR; _map_->flag = CMC_FLAG_ERROR;
return false; return false;
@@ -714,8 +714,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity)
_new_map_->capacity = tmp_c; _new_map_->capacity = tmp_c;
/* Prevent the map from freeing the data */ /* Prevent the map from freeing the data */
_new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 };
_new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
CMC_(PFX, _free)(_new_map_); CMC_(PFX, _free)(_new_map_);
+3 -3
View File
@@ -41,7 +41,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE
return NULL; return NULL;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return NULL; return NULL;
if (!f_val) if (!f_val)
@@ -443,7 +443,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity)
goto success; goto success;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * _set_->load) if (capacity >= (double)UINTMAX_MAX * _set_->load)
{ {
_set_->flag = CMC_FLAG_ERROR; _set_->flag = CMC_FLAG_ERROR;
return false; return false;
@@ -500,7 +500,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity)
_new_set_->capacity = tmp_c; _new_set_->capacity = tmp_c;
/* Prevent the set from freeing the data */ /* Prevent the set from freeing the data */
_new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
CMC_(PFX, _free)(_new_set_); CMC_(PFX, _free)(_new_set_);
+3 -3
View File
@@ -40,7 +40,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE
return NULL; return NULL;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * load) if (capacity >= (double)UINTMAX_MAX * load)
return NULL; return NULL;
if (!f_val) if (!f_val)
@@ -346,7 +346,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity)
goto success; goto success;
/* Prevent integer overflow */ /* Prevent integer overflow */
if (capacity >= UINTMAX_MAX * _set_->load) if (capacity >= (double)UINTMAX_MAX * _set_->load)
{ {
_set_->flag = CMC_FLAG_ERROR; _set_->flag = CMC_FLAG_ERROR;
return false; return false;
@@ -400,7 +400,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity)
_new_set_->capacity = tmp_c; _new_set_->capacity = tmp_c;
/* Prevent the set from freeing the data */ /* Prevent the set from freeing the data */
_new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 };
CMC_(PFX, _free)(_new_set_); CMC_(PFX, _free)(_new_set_);