Code cleanup in the List code. Possibly saved myself some headache in the future by using the right pointer type in the struct definition.
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
#define LISTDEFAULTSIZE 4
|
#define LISTDEFAULTSIZE 4
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char** root;
|
void** content;
|
||||||
int size;
|
int size;
|
||||||
int capacity;
|
int capacity;
|
||||||
} List;
|
} List;
|
||||||
|
|||||||
+9
-9
@@ -8,9 +8,9 @@ List* CreateList() {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
new->root = malloc(sizeof(void*) * LISTDEFAULTSIZE);
|
new->content = malloc(sizeof(void*) * LISTDEFAULTSIZE);
|
||||||
|
|
||||||
if (!new->root) {
|
if (!new->content) {
|
||||||
fprintf(stderr, "Failed to malloc() memory for List contents.\n");
|
fprintf(stderr, "Failed to malloc() memory for List contents.\n");
|
||||||
free(new);
|
free(new);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -28,27 +28,27 @@ int AddListItem(const void *value, size_t size, List* list) {
|
|||||||
if (size == 0) return - 1;
|
if (size == 0) return - 1;
|
||||||
|
|
||||||
if (list->capacity < list->size + 1) {
|
if (list->capacity < list->size + 1) {
|
||||||
void* ptr = realloc(list->root, sizeof(void*) * list->capacity * 2);
|
void* ptr = realloc(list->content, sizeof(void*) * list->capacity * 2);
|
||||||
//Note: realloc will free list->root if it succeeds.
|
//Note: realloc will free list->root if it succeeds.
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2);
|
fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
list->root = ptr;
|
list->content = ptr;
|
||||||
list->capacity = list->capacity * 2;
|
list->capacity = list->capacity * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* item = malloc(size);
|
void* item = calloc(1, size);
|
||||||
|
|
||||||
if (!item) {
|
if (!item) {
|
||||||
fprintf(stderr, "Failed to malloc() new memory (%lu bytes).\n", size);
|
fprintf(stderr, "Failed to calloc() new memory (%lu bytes).\n", size);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(item, value, size);
|
memcpy(item, value, size);
|
||||||
|
|
||||||
list->root[list->size] = item;
|
list->content[list->size] = item;
|
||||||
list->size++;
|
list->size++;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -58,9 +58,9 @@ void DestroyList(List* list) {
|
|||||||
if (!list) return;
|
if (!list) return;
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
for(int i = 0; i < list->size; i++) {
|
||||||
free(list->root[i]);
|
free(list->content[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(list->root);
|
free(list->content);
|
||||||
free(list);
|
free(list);
|
||||||
}
|
}
|
||||||
+2
-13
@@ -42,11 +42,12 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
||||||
|
|
||||||
printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->root[list->size - 1])->SomeValue, ((struct test*) list->root[list->size - 1])->SomeText);
|
printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->content[list->size - 1])->SomeValue, ((struct test*) list->content[list->size - 1])->SomeText);
|
||||||
}
|
}
|
||||||
|
|
||||||
//PrintList(list);
|
//PrintList(list);
|
||||||
free(thing->SomeText);
|
free(thing->SomeText);
|
||||||
|
free(thing);
|
||||||
|
|
||||||
free(list);
|
free(list);
|
||||||
}
|
}
|
||||||
@@ -130,16 +131,4 @@ List* get_strings(const char* line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
|
||||||
|
|
||||||
void PrintList(const List* list) {
|
|
||||||
if (!list) return;
|
|
||||||
|
|
||||||
const List *current = list;
|
|
||||||
|
|
||||||
printf("Size: %d; Capacity: %d\n", list->size, list->capacity);
|
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
|
||||||
printf("%d: %s\n", i, list->root[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user