From c83ba3422aa569cbc8bfdc38bdd1b413526e713e Mon Sep 17 00:00:00 2001 From: Eric Patrizio Date: Sun, 27 Feb 2022 10:51:10 +0100 Subject: [PATCH] Bug fix vector memory allocation --- Makefile | 8 +++++++- src/vector.h | 16 ++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index ba82cc9..1b7ef59 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,12 @@ compile_test: tests/tests_utils.c tests/tests_hash_set.c tests/tests_hash_map.c tests/tests_graph.c tests/main.c \ -lm -L./tests/lib -lctestfmk +run_test: + ./cds_test + +run_test_debug: + valgrind ./cds_test + compile_example_intl: gcc -g -o intl -Wall -Wextra \ src/vector.c src/hash_utils.c src/hash_map.c examples/intl/intl.c examples/intl/main.c @@ -28,7 +34,7 @@ clean: # Adjust src/*.c list to compile according to the data structures to be used elsewhere libcds_static: mkdir -p lib - gcc -c -Wall -Wextra src/vector.c + gcc -c -Wall -Wextra -O3 src/vector.c mv vector.o lib/vector.o cp src/vector.h lib/vector.h ar -rc lib/libvector.a lib/*.o diff --git a/src/vector.h b/src/vector.h index cf0c1b5..e5bffb9 100644 --- a/src/vector.h +++ b/src/vector.h @@ -29,14 +29,14 @@ vector_struct(T) \ vector_create_sign(T) -#define vector_create_fct(T) \ - T##_vector T##_vector_create(size_t capacity) { \ - size_t cap = (capacity > 0) ? capacity : MIN_CAPACITY; \ - T##_vector v = malloc(sizeof(T##_vector)); \ - v->elts = malloc(cap * sizeof(T)); \ - v->capacity = cap; \ - v->size = 0; \ - return v; \ +#define vector_create_fct(T) \ + T##_vector T##_vector_create(size_t capacity) { \ + size_t cap = (capacity > 0) ? capacity : MIN_CAPACITY; \ + T##_vector v = malloc(sizeof(struct T##_resizable_array)); \ + v->elts = malloc(cap * sizeof(T)); \ + v->capacity = cap; \ + v->size = 0; \ + return v; \ } #define vector_destroy_fct(T) \