Fix memory errors detected by valgrind and inline functions
[satune.git] / src / Collections / vector.h
index fa1764e07a0405409c29359eb56310bf22754992..76da3873dcc633ca553926c9b1bd36725e88f238 100644 (file)
@@ -29,7 +29,7 @@
                return allocVector ## name(defcap);                                 \
        }                                                                     \
        Vector ## name * allocVector ## name(uint capacity) {                 \
-               Vector ## name * tmp = (Vector ## name *)ourmalloc(sizeof(type));  \
+               Vector ## name * tmp = (Vector ## name *)ourmalloc(sizeof(Vector ## name));  \
                tmp->size = 0;                                                      \
                tmp->capacity = capacity;                                           \
                tmp->array = (type *) ourcalloc(1, sizeof(type) * capacity);          \
@@ -37,7 +37,8 @@
        }                                                                     \
        Vector ## name * allocVectorArray ## name(uint capacity, type * array)  { \
                Vector ## name * tmp = allocVector ## name(capacity);               \
-               memcpy(tmp->array, array, capacity * sizeof(type));                 \
+               tmp->size=capacity;                                                                                                                                                                                                     \
+               memcpy(tmp->array, array, capacity * sizeof(type));                                                                     \
                return tmp;                                                         \
        }                                                                     \
        void pushVector ## name(Vector ## name *vector, type item) {          \
@@ -79,6 +80,7 @@
        }                                                                                                                                                                                                                                                                                       \
        void allocInlineVectorArray ## name(Vector ## name * vector, uint capacity, type * array) {     \
                allocInlineVector ##name(vector, capacity);                                                                                                     \
-               memcpy(vector->array, array, capacity * sizeof(type));                                                  \
+               vector->size=capacity;                                                                                                                                                                                  \
+               memcpy(vector->array, array, capacity * sizeof(type));  \
        }
 #endif