diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca29fca5..6a9c91cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,11 +7,19 @@ on: jobs: make-check: runs-on: ubuntu-latest + strategy: + matrix: + compiler: [ + {cc: gcc, std: c99}, + {cc: clang, std: c23}, + {cc: g++, std: c++23}, + {cc: clang++, std: c++23} + ] steps: - uses: actions/checkout@v3 - name: Run make check - run: make check + run: make check CC=${{ matrix.compiler.cc }} STD=${{ matrix.compiler.std }} make-check-valgrind: runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 0739fdf6..135af950 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,14 @@ else endif CC = gcc +STD = c99 WARN = -Wall -Wextra DEBUG ?= -g SRCDIR = src LIBNAME = libds-lerax LIBDIR = lib HEADER = ds-lerax.h -override CFLAGS += $(DEBUG) -pedantic $(WARN) -std=c99 -fPIC +override CFLAGS += $(DEBUG) -pedantic $(WARN) -std=$(STD) -fPIC SOURCES = $(shell find $(SRCDIR) -iname '*.c') COMPILED = $(shell find $(SRCDIR) -type f -iname '*.o' -or -iname "*.out" -or -iname "*.a") TEST_TRASH = $(shell find $(SRCDIR) -type f -iname 'test*.dot*') @@ -107,6 +108,9 @@ test: all check: test +check-cpp: + make check CC=g++ STD=c++23 + check/%: make test -C src/$* diff --git a/src/circle/Makefile b/src/circle/Makefile index 6802fbe8..318ceeb0 100644 --- a/src/circle/Makefile +++ b/src/circle/Makefile @@ -1,5 +1,6 @@ CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -lm TARGET = circle OBJ = $(TARGET).o diff --git a/src/graph/Makefile b/src/graph/Makefile index 4d47267b..98890acc 100644 --- a/src/graph/Makefile +++ b/src/graph/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS := -lset -lhash-table -llist -lqueue -lstack -lpqueue -lm INCLUDE := -I../ -L../list/single -L../hash-table -L../set -L../queue -L../stack -L../pqueue diff --git a/src/graph/acyclical.c b/src/graph/acyclical.c index 71b5364b..2d3e80b3 100644 --- a/src/graph/acyclical.c +++ b/src/graph/acyclical.c @@ -48,13 +48,13 @@ static void graph_dfsa( } struct CycleContext* cycle_context_create(Graph *g) { - struct CycleContext *cc = malloc(sizeof(struct CycleContext)); + struct CycleContext *cc = (struct CycleContext *) malloc(sizeof(struct CycleContext)); int max_nodes = graph_max_node_id(g) + 1; cc->topological_sort = stack_create(); cc->cycle_path = stack_create(); - cc->exploration = malloc(max_nodes * sizeof(int)); - cc->complete = malloc(max_nodes * sizeof(int)); + cc->exploration = (int*) malloc(max_nodes * sizeof(int)); + cc->complete = (int*) malloc(max_nodes * sizeof(int)); cc->counter_complete = 0; cc->counter_exploration = 0; cc->has_cycles = false; diff --git a/src/graph/bfs.c b/src/graph/bfs.c index a5b81c32..c2e013ab 100644 --- a/src/graph/bfs.c +++ b/src/graph/bfs.c @@ -77,7 +77,7 @@ Iterator* graph_bfs(Graph* g, int start_node) { queue_insert(q, start_node); set_add(visited, start_node); - GraphIteratorContext* it_context = malloc(sizeof(GraphIteratorContext)); + GraphIteratorContext* it_context = (GraphIteratorContext*) malloc(sizeof(GraphIteratorContext)); it_context->graph = g; it_context->nodes = graph_nodes_iterator(g); it_context->visited = visited; diff --git a/src/graph/dfs.c b/src/graph/dfs.c index 0248c1cc..7c174405 100644 --- a/src/graph/dfs.c +++ b/src/graph/dfs.c @@ -77,7 +77,7 @@ Iterator* graph_dfs(Graph* g, int start_node) { stack_push(s, start_node); set_add(visited, start_node); - GraphIteratorContext* it_context = malloc(sizeof(GraphIteratorContext)); + GraphIteratorContext* it_context = (GraphIteratorContext*) malloc(sizeof(GraphIteratorContext)); it_context->graph = g; it_context->nodes = graph_nodes_iterator(g); it_context->visited = visited; diff --git a/src/graph/dijkstra.c b/src/graph/dijkstra.c index 6d6e3476..9df5372a 100644 --- a/src/graph/dijkstra.c +++ b/src/graph/dijkstra.c @@ -20,8 +20,8 @@ Graph* graph_dijkstra(Graph* g, int source) { int n = graph_max_node_id(g) + 1; - int dist[n]; - int prev[n]; + int *dist = (int*) malloc(sizeof(int) * n); + int *prev = (int*) malloc(sizeof(int) * n); for (int i = 0; i < n; i++) { dist[i] = DIJKSTRA_INFINITY; @@ -63,6 +63,8 @@ Graph* graph_dijkstra(Graph* g, int source) { } pqueue_free(pq); + free(dist); + free(prev); return g_new; } diff --git a/src/graph/graph.c b/src/graph/graph.c index 5a3c4bf6..0dd62dbd 100644 --- a/src/graph/graph.c +++ b/src/graph/graph.c @@ -13,7 +13,7 @@ struct Graph { }; Graph* graph_create() { - Graph *g = malloc(sizeof(Graph)); + Graph *g = (Graph*) malloc(sizeof(Graph)); if (!g) { return NULL; } @@ -43,7 +43,7 @@ Graph* graph_tarjan_create(bool directed) { } static void graph_nodes_iterator_free(Iterator *it) { - list_free(it->begin); + list_free((List*)it->begin); free(it); } @@ -108,12 +108,12 @@ void graph_add_node(Graph *g, int node) { void graph_add_edge_with_weight(Graph *g, int u, int v, int weight) { graph_add_node(g, u); graph_add_node(g, v); - Set *set_u = hash_table_gen_get(g->adj, u, NULL); + Set *set_u = (Set*) hash_table_gen_get(g->adj, u, NULL); set_add_with_value(set_u, v, weight); g->weighted = true; if (!g->directed) { - Set *set_v = hash_table_gen_get(g->adj, v, NULL); + Set *set_v = (Set*) hash_table_gen_get(g->adj, v, NULL); set_add_with_value(set_v, u, weight); } } @@ -121,18 +121,18 @@ void graph_add_edge_with_weight(Graph *g, int u, int v, int weight) { void graph_add_edge(Graph *g, int u, int v) { graph_add_node(g, u); graph_add_node(g, v); - Set *set_u = hash_table_gen_get(g->adj, u, NULL); + Set *set_u = (Set*) hash_table_gen_get(g->adj, u, NULL); set_add(set_u, v); if (!g->directed) { - Set *set_v = hash_table_gen_get(g->adj, v, NULL); + Set *set_v = (Set*) hash_table_gen_get(g->adj, v, NULL); set_add(set_v, u); } } void graph__remove_edge(Graph *g, int u, int v) { bool u_exists, v_exists; - Set *set_u = hash_table_gen_get(g->adj, u, &u_exists); + Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &u_exists); hash_table_gen_get(g->adj, v, &v_exists); if (u_exists && v_exists) { @@ -149,7 +149,7 @@ void graph_remove_edge(Graph *g, int u, int v) { void graph_remove_node(Graph *g, int node) { bool exists; - Set *adj_node = hash_table_gen_get(g->adj, node, &exists); + Set *adj_node = (Set*) hash_table_gen_get(g->adj, node, &exists); if (exists) { set_free(adj_node); hash_table_gen_remove(g->adj, node); @@ -159,7 +159,7 @@ void graph_remove_node(Graph *g, int node) { Iterator *it = list_iterator_data(nodes); while(!iterator_done(it)) { int u = *(int*)iterator_next(it); - Set *s = hash_table_gen_get(g->adj, u, NULL); + Set *s = (Set*) hash_table_gen_get(g->adj, u, NULL); set_remove(s, node); } list_free(nodes); @@ -168,7 +168,7 @@ void graph_remove_node(Graph *g, int node) { bool graph_has_edge(Graph *g, int u, int v) { bool exists; - Set *set_u = hash_table_gen_get(g->adj, u, &exists); + Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &exists); if (!exists) { return false; } @@ -177,7 +177,7 @@ bool graph_has_edge(Graph *g, int u, int v) { Set* graph_get_neighbors(Graph *g, int node) { bool exists; - Set *neighbors = hash_table_gen_get(g->adj, node, &exists); + Set *neighbors = (Set*) hash_table_gen_get(g->adj, node, &exists); if (!exists) { return set_create(); } @@ -186,7 +186,7 @@ Set* graph_get_neighbors(Graph *g, int node) { int graph_get_edge_weight(Graph *g, int u, int v) { bool exists; - Set *set_u = hash_table_gen_get(g->adj, u, &exists); + Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &exists); if (!exists) { return -1; } @@ -200,14 +200,13 @@ void graph_print(Graph *g) { Iterator *it = graph_nodes_iterator(g); while(!iterator_done(it)) { int u = *(int*)iterator_next(it); - printf("%d: ", u); - Set *neighbors = hash_table_gen_get(g->adj, u, NULL); + Set *neighbors = (Set*) hash_table_gen_get(g->adj, u, NULL); if (g->tarjan) { Iterator *it = set_iterator_items(neighbors); printf("{"); while (!iterator_done(it)) { List *list = (List*) iterator_next(it); - printf("%d:%s", list->key, graph_edge_type_name(list->data)); + printf("%d:%s", list->key, graph_edge_type_name((EdgeType)list->data)); if (!iterator_done(it)) { printf(", "); } @@ -252,7 +251,7 @@ void graph_export_to_dot(Graph *g, const char* filename) { current_node = nodes; while (current_node) { int u = (int)(long)current_node->data; - Set *neighbors = hash_table_gen_get(g->adj, u, NULL); + Set *neighbors = (Set*) hash_table_gen_get(g->adj, u, NULL); Iterator *it = set_iterator(neighbors); while (!iterator_done(it)) { int v = *(int*)iterator_next(it); @@ -260,7 +259,7 @@ void graph_export_to_dot(Graph *g, const char* filename) { fprintf(fp, " %d %s %d", u, g->directed ? "->" : "--", v); if (g->tarjan) { int edge_type = set_get_value(neighbors, v); - fprintf(fp, " [label=\"%s\"]", graph_edge_type_name(edge_type)); + fprintf(fp, " [label=\"%s\"]", graph_edge_type_name((EdgeType)edge_type)); } else if (g->weighted) { int weight = set_get_value(neighbors, v); fprintf(fp, " [label=\"%d\"]", weight); diff --git a/src/graph/tarjan.c b/src/graph/tarjan.c index b7bc30e2..918d6376 100644 --- a/src/graph/tarjan.c +++ b/src/graph/tarjan.c @@ -52,8 +52,14 @@ static void graph_dfst( Graph* graph_tarjan(Graph *g) { struct TarjanContext tc; int max_node_id = graph_max_node_id(g); - int exploration[max_node_id]; - int complete[max_node_id]; + int *exploration = (int*) malloc(sizeof(int) * (max_node_id + 1)); + for (int i = 0; i <= max_node_id; i++) { + exploration[i] = 0; + } + int *complete = (int*) malloc(sizeof(int) * (max_node_id + 1)); + for (int i = 0; i <= max_node_id; i++) { + complete[i] = 0; + } tc.g_tarjan = graph_tarjan_create(graph_is_directed(g)); tc.counter_complete = 0; @@ -61,12 +67,6 @@ Graph* graph_tarjan(Graph *g) { tc.exploration = exploration; tc.complete = complete; - // initialize arrays - for (int i = 0; i <= max_node_id; i++) { - exploration[i] = 0; - complete[i] = 0; - } - // dfs over each node Iterator *nodes = graph_nodes_iterator(g); while (!iterator_done(nodes)) { @@ -77,5 +77,7 @@ Graph* graph_tarjan(Graph *g) { } iterator_free(nodes); + free(exploration); + free(complete); return tc.g_tarjan; } diff --git a/src/hash-table/Makefile b/src/hash-table/Makefile index fdb67e83..929ccec4 100644 --- a/src/hash-table/Makefile +++ b/src/hash-table/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS := -llist -lm INCLUDE := -I../ -L../list/single diff --git a/src/hash-table/hash-table-gen.c b/src/hash-table/hash-table-gen.c index 190f38ed..4d55edfc 100644 --- a/src/hash-table/hash-table-gen.c +++ b/src/hash-table/hash-table-gen.c @@ -17,12 +17,12 @@ static unsigned int hash_int(int key, size_t n_buckets) { } HashTableGen* hash_table_gen_create(size_t n_buckets) { - HashTableGen *ht = malloc(sizeof(HashTableGen)); + HashTableGen *ht = (HashTableGen*) malloc(sizeof(HashTableGen)); if (!ht) return NULL; ht->n_buckets = n_buckets; ht->size = 0; - ht->buckets = calloc(n_buckets, sizeof(ListGen*)); + ht->buckets = (ListGen**) calloc(n_buckets, sizeof(ListGen*)); if (!ht->buckets) { free(ht); return NULL; diff --git a/src/hash-table/hash-table.c b/src/hash-table/hash-table.c index 174b6e16..f7f3ddef 100644 --- a/src/hash-table/hash-table.c +++ b/src/hash-table/hash-table.c @@ -18,12 +18,12 @@ static unsigned int hash_int(int key, size_t n_buckets) { } HashTable* hash_table_create(size_t n_buckets) { - HashTable *ht = malloc(sizeof(HashTable)); + HashTable *ht = (HashTable*) malloc(sizeof(HashTable)); check_alloc(ht); ht->n_buckets = n_buckets; ht->size = 0; - ht->buckets = calloc(n_buckets, sizeof(List*)); + ht->buckets = (List**) calloc(n_buckets, sizeof(List*)); if (!ht->buckets) { free(ht); return NULL; @@ -182,7 +182,7 @@ void hash_table_free(HashTable *ht) { } static void hash_table_iterator_free(Iterator *it) { - list_free(it->begin); + list_free((List*) it->begin); free(it); } diff --git a/src/iterator/iterator.h b/src/iterator/iterator.h index 4de1e611..07119d59 100644 --- a/src/iterator/iterator.h +++ b/src/iterator/iterator.h @@ -33,7 +33,7 @@ static inline Iterator* iterator_create( void (*free)(Iterator*), bool (*done)(Iterator*) ) { - Iterator* it = malloc(sizeof(Iterator)); + Iterator* it = (Iterator*) malloc(sizeof(Iterator)); check_alloc(it); it->container = container; it->begin = container; diff --git a/src/list/circular/Makefile b/src/list/circular/Makefile index 6802f10f..b5d42864 100644 --- a/src/list/circular/Makefile +++ b/src/list/circular/Makefile @@ -5,7 +5,8 @@ else endif CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -lm LIST_CIRCULAR = list-circular TEST_TARGET = test diff --git a/src/list/double/Makefile b/src/list/double/Makefile index 190a4835..82f4e486 100644 --- a/src/list/double/Makefile +++ b/src/list/double/Makefile @@ -5,7 +5,8 @@ else endif CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = --static -lm LIST_DOUBLE = list-double TEST_TARGET = test diff --git a/src/list/single/Makefile b/src/list/single/Makefile index 8a868b16..74966070 100644 --- a/src/list/single/Makefile +++ b/src/list/single/Makefile @@ -5,7 +5,8 @@ else endif CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -lm TEST_TARGET = test diff --git a/src/list/single/list-gen.c b/src/list/single/list-gen.c index 2e13dc43..1e049515 100644 --- a/src/list/single/list-gen.c +++ b/src/list/single/list-gen.c @@ -11,24 +11,24 @@ bool list_gen_empty(ListGen *l) { } ListGen* list_gen_insert(ListGen *l, void *data) { - ListGen *new = (ListGen*) malloc(sizeof(ListGen)); - if (new == NULL) { + ListGen *new_node = (ListGen*) malloc(sizeof(ListGen)); + if (new_node == NULL) { exit(1); } - new->data = data; - new->next = l; - return new; + new_node->data = data; + new_node->next = l; + return new_node; } ListGen* list_gen_insert_with_key(ListGen *l, int key, void *data) { - ListGen *new = (ListGen*) malloc(sizeof(ListGen)); - if (new == NULL) { + ListGen *new_node = (ListGen*) malloc(sizeof(ListGen)); + if (new_node == NULL) { exit(1); } - new->key = key; - new->data = data; - new->next = l; - return new; + new_node->key = key; + new_node->data = data; + new_node->next = l; + return new_node; } ListGen* list_gen_remove(ListGen *l, void *data) { diff --git a/src/matrix/Makefile b/src/matrix/Makefile index 477bef3b..dc4ac9a6 100644 --- a/src/matrix/Makefile +++ b/src/matrix/Makefile @@ -1,5 +1,6 @@ CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -lm MATRIX_POINTER = matrix-pointer MATRIX_VECTOR = matrix-vector diff --git a/src/point/Makefile b/src/point/Makefile index 773ca17d..d3ea22c5 100644 --- a/src/point/Makefile +++ b/src/point/Makefile @@ -1,5 +1,6 @@ CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -lm TARGET = point OBJ = $(TARGET).o diff --git a/src/pqueue/Makefile b/src/pqueue/Makefile index 0957f669..ab0eb6c9 100644 --- a/src/pqueue/Makefile +++ b/src/pqueue/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS = -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS = -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS = -lhash-table -llist -lm INCLUDE = -I../ -L../list/single -L../hash-table diff --git a/src/pqueue/pqueue.c b/src/pqueue/pqueue.c index b03fbbc4..f8dff4e9 100644 --- a/src/pqueue/pqueue.c +++ b/src/pqueue/pqueue.c @@ -115,7 +115,8 @@ void pqueue_insert(PQueue *pq, int key, int value) { } pq->size++; int i = pq->size - 1; - pq->heap[i] = (PQueueNode){key, value}; + pq->heap[i].key = key; + pq->heap[i].value = value; hash_table_put(pq->index, key, i); @@ -137,7 +138,8 @@ void pqueue_insert(PQueue *pq, int key, int value) { PQueueNode pqueue_top(PQueue *pq) { if (pq->size < 1) { printf("Heap underflow!\n"); - return HEAP_EMPTY_NODE; + PQueueNode empty_node = HEAP_EMPTY_NODE; + return empty_node; } return pq->heap[0]; } @@ -145,7 +147,8 @@ PQueueNode pqueue_top(PQueue *pq) { PQueueNode pqueue_extract(PQueue *pq) { if (pq->size < 1) { printf("Heap underflow!\n"); - return HEAP_EMPTY_NODE; + PQueueNode empty_node = HEAP_EMPTY_NODE; + return empty_node; } PQueueNode top_node = pq->heap[0]; pq->heap[0] = pq->heap[pq->size - 1]; @@ -270,7 +273,7 @@ void pqueue_iterator_free(Iterator *it) { Iterator* pqueue_iterator(PQueue *pq) { - struct PQueueIterator *pq_it = malloc(sizeof(struct PQueueIterator)); + struct PQueueIterator *pq_it = (struct PQueueIterator*) malloc(sizeof(struct PQueueIterator)); check_alloc(pq_it); pq_it->pq = pq; pq_it->index = 0; @@ -284,7 +287,7 @@ Iterator* pqueue_iterator(PQueue *pq) { Iterator* pqueue_iterator_keys(PQueue *pq) { - struct PQueueIterator *pq_it = malloc(sizeof(struct PQueueIterator)); + struct PQueueIterator *pq_it = (struct PQueueIterator*) malloc(sizeof(struct PQueueIterator)); check_alloc(pq_it); pq_it->pq = pq; pq_it->index = 0; diff --git a/src/pqueue/pqueue.h b/src/pqueue/pqueue.h index 159d9a96..338b33cd 100644 --- a/src/pqueue/pqueue.h +++ b/src/pqueue/pqueue.h @@ -34,7 +34,7 @@ typedef struct PQueueNode { } PQueueNode; -#define HEAP_EMPTY_NODE (PQueueNode){-1, -1} +#define HEAP_EMPTY_NODE {.key=-1, .value=-1} /** * @brief Enum for priority queue type (min or max). diff --git a/src/queue/Makefile b/src/queue/Makefile index 4de841b5..4de9ce95 100644 --- a/src/queue/Makefile +++ b/src/queue/Makefile @@ -5,7 +5,8 @@ else endif CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -llist -lm QUEUE_STATIC = queue-static QUEUE_DYNAMIC = queue-dynamic diff --git a/src/search/Makefile b/src/search/Makefile index 9b56d8e4..c3a8b4ba 100644 --- a/src/search/Makefile +++ b/src/search/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS := -lm INCLUDE := -I../ diff --git a/src/search/search.c b/src/search/search.c index e4ed1039..10ddb541 100644 --- a/src/search/search.c +++ b/src/search/search.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include "search.h" #include "../utils/prime.h" @@ -54,15 +54,16 @@ void kmp_init_failure_function(int *f, int m, const char *pattern) { int search_kmp(char *text, const char *pattern) { int n = strlen(text); int m = strlen(pattern); - int f[m]; + int *f = (int*) malloc(sizeof(int) * m); kmp_init_failure_function(f, m, pattern); int i = 0, j = 0; - + int search_index = -1; while (i < n) { if (text[i] == pattern[j]) { if (j == (m - 1)) { - return i - j; + search_index = i - j; + break; } else { i++; j++; @@ -75,7 +76,8 @@ int search_kmp(char *text, const char *pattern) { } } } - return -1; + free(f); + return search_index; } void bm_init_l(int *l, int m, const char* pattern) { diff --git a/src/search/test.c b/src/search/test.c index 6932bdac..3d58e69d 100644 --- a/src/search/test.c +++ b/src/search/test.c @@ -16,9 +16,9 @@ #include "search.h" struct test_case { - char *text; - char *pattern; - int expected; + const char *text; + const char *pattern; + const int expected; }; @@ -62,7 +62,7 @@ int answer, i; printf("== Testing for %s: ", #ALGORITHM); \ for (i = 0; i < n_tests; i++) { \ printf("\n Input: text='%s'; \t pattern='%s'\n", tests[i].text, tests[i].pattern); \ - answer = ALGORITHM(tests[i].text, tests[i].pattern); \ + answer = ALGORITHM((char*) tests[i].text, (char*)tests[i].pattern); \ printf(" Result: answer=%d; expected=%d\n", answer, tests[i].expected); \ assert(answer == tests[i].expected); \ }; diff --git a/src/set/Makefile b/src/set/Makefile index 68e0f16e..d536de3c 100644 --- a/src/set/Makefile +++ b/src/set/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS := -lhash-table -llist -lm INCLUDE := -I../ -L../list/single -L../hash-table/ diff --git a/src/set/set.c b/src/set/set.c index e325ffdb..c7920b5c 100644 --- a/src/set/set.c +++ b/src/set/set.c @@ -15,7 +15,7 @@ struct Set { Set* set_create() { - Set *set = malloc(sizeof(Set)); + Set *set = (Set*) malloc(sizeof(Set)); check_alloc(set); set->memory = hash_table_create(SET_DEFAULT_HASH_MAP_SIZE); return set; diff --git a/src/sort/Makefile b/src/sort/Makefile index c87f4f90..05a28e7e 100644 --- a/src/sort/Makefile +++ b/src/sort/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS := -lm -lqueue -llist INCLUDE := -I../ -L../queue/ -L../list/single/ diff --git a/src/sort/mergesort.c b/src/sort/mergesort.c index 9653a8ed..b5220f84 100644 --- a/src/sort/mergesort.c +++ b/src/sort/mergesort.c @@ -11,6 +11,7 @@ */ #include "sort.h" +#include /** * @description Merges two partitions in sorted way @@ -29,7 +30,7 @@ void merge(Type v[], int l, int m, int r) { int k = m + 1; int n = r - l + 1; - Type aux[n]; + Type *aux = (Type*) malloc(sizeof(Type) * n); // merge ordered elements while (j <= m && k <= r) { if (v[j] < v[k]) { @@ -52,6 +53,7 @@ void merge(Type v[], int l, int m, int r) { for (int x = l; x <= r; x++) { v[x] = aux[x - l]; } + free(aux); } diff --git a/src/sort/utils.c b/src/sort/utils.c index 134c9238..39b7e1f5 100644 --- a/src/sort/utils.c +++ b/src/sort/utils.c @@ -32,7 +32,7 @@ void print_vector(Type *v, int n) { Type* random_vector(int n) { Type* v = (Type*) malloc(sizeof(Type) * n); check_alloc(v); - srand(time((void*) v)); + srand(time(NULL)); for (int i = 0; i < n; i++) { v[i] = rand() % n; diff --git a/src/stack/Makefile b/src/stack/Makefile index 0d1b8f52..35f65c29 100644 --- a/src/stack/Makefile +++ b/src/stack/Makefile @@ -5,7 +5,8 @@ else endif CC = gcc -override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99 +STD = c99 +override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD) LDFLAGS = -llist -lm STACK_STATIC = stack-static STACK_DYNAMIC = stack-dynamic diff --git a/src/tree/Makefile b/src/tree/Makefile index 6221fed6..5c95335f 100644 --- a/src/tree/Makefile +++ b/src/tree/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS = -lm -L./ascii-tree/ -L./avl/ -lavl -lascii-tree LDFLAGS = -L./ascii-tree/ -L./avl/ -B static -lavl -lascii-tree -B dynamic -lm INCLUDE = -I../ diff --git a/src/tree/ascii-tree/Makefile b/src/tree/ascii-tree/Makefile index 1fa83584..5d7aaed0 100644 --- a/src/tree/ascii-tree/Makefile +++ b/src/tree/ascii-tree/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS = -L../bst/ -lbst -lm ASCII_TREE = ascii-tree TEST_TARGET = test diff --git a/src/tree/ascii-tree/ascii-tree.c b/src/tree/ascii-tree/ascii-tree.c index f5468dd6..6b85c94e 100644 --- a/src/tree/ascii-tree/ascii-tree.c +++ b/src/tree/ascii-tree/ascii-tree.c @@ -63,7 +63,7 @@ ASCIITree *build_ascii_tree_recursive(BinaryTree *t) { if (t == NULL) return NULL; - node = malloc(sizeof(ASCIITree)); + node = (ASCIITree*) malloc(sizeof(ASCIITree)); node->left = build_ascii_tree_recursive(t->left); node->right = build_ascii_tree_recursive(t->right); diff --git a/src/tree/avl/Makefile b/src/tree/avl/Makefile index 0c15859f..61844f20 100644 --- a/src/tree/avl/Makefile +++ b/src/tree/avl/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS = -L../ascii-tree/ -lm -lascii-tree AVL = avl TEST_TARGET = test diff --git a/src/tree/avl/avl.c b/src/tree/avl/avl.c index 54614622..bb73217d 100644 --- a/src/tree/avl/avl.c +++ b/src/tree/avl/avl.c @@ -30,7 +30,7 @@ static int height(AVLTree* node) { } AVLTree* avl_create_node(Type value) { - AVLTree* node = malloc(sizeof(AVLTree)); + AVLTree* node = (AVLTree*) malloc(sizeof(AVLTree)); node->value = value; node->left = NULL; node->right = NULL; diff --git a/src/tree/bst/Makefile b/src/tree/bst/Makefile index 80254992..8b7bb227 100644 --- a/src/tree/bst/Makefile +++ b/src/tree/bst/Makefile @@ -7,7 +7,8 @@ endif CC = gcc WARN = -Wall -Wextra DEBUG = -g -override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99 +STD = c99 +override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD) LDFLAGS = -L../ascii-tree/ -lm -lascii-tree BST = bst TEST_TARGET = test