Refactor package module into C #6

Open
GreekStapler wants to merge 25 commits from GreekStapler/libvieter:package into dev
2 changed files with 8 additions and 8 deletions
Showing only changes of commit 01688fd546 - Show all commits

View file

@ -5,6 +5,11 @@
#include <string.h>
typedef struct dyn_array DynArray;
struct dyn_array {
char **array;
size_t capacity;
size_t size;
};
DynArray *dynarray_init(size_t initial_capacity);
void dynarray_add(DynArray *da, const char * s);

View file

@ -1,11 +1,5 @@
#include "dynarray.h"
struct dyn_array {
char **array;
size_t size;
size_t capacity;
};
DynArray *dynarray_init(size_t initial_capacity) {
DynArray *da = malloc(sizeof(DynArray));
da->size = 0;
@ -21,7 +15,8 @@ void dynarray_add(DynArray *da, const char *s) {
}
// Double array size if it's full
else if (da->size == da->capacity) {
da->array = realloc(da->array, da->capacity * 2);
// if the realloc fails, access to memory in da->array is lost
da->array = realloc(da->array, sizeof(char*) * da->capacity * 2);
da->capacity *= 2;
}