#freeing memory in a struct Generic ArrayList of. void ** and void* objects
4 messages · Page 1 of 1 (latest)
`#include "ArrayList.h"
#define MAX_CAP 10;
/* Constructs an empty list with an initial capacity of ten. */
ArrayListPtr CreateArrayList(int (*compareTo)(const void *, const void *), char *(*toString)(const void *), void (*freeObject)(void *)){
ArrayListPtr list = (ArrayListPtr) malloc(sizeof(ArrayList));
if(list == NULL){
return NULL;
}
list->size = 0;
list->maxSize = MAX_CAP;
list->array = (void**) malloc(list->maxSize * sizeof(void*));
list->compareTo = compareTo;
list->toString = toString;
list->freeObject = freeObject;
return list;
}
/* frees all memory associated with the arraylist */
void FreeArrayList(ArrayListPtr A){
if( A != NULL ){
if(A->freeObject != NULL){
for(int i = 0; i < A->size; i++){
A->freeObject(A->array[i]);
}
}
free(A->array);
free(A);
}
}`
this is part of my generic arraylist
`this is .h for that file:
#define ARRAY_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DEFAULT_SIZE 8
typedef struct arraylist ArrayList;
typedef struct arraylist *ArrayListPtr;
struct arraylist {
int size;
int maxSize;
void **array;
int (*compareTo)(const void *, const void *);
char *(*toString)(const void *);
void (*freeObject)(void *);
};
/* Constructs an empty list with an initial capacity of ten. */
ArrayListPtr CreateArrayList(int (*compareTo)(const void *, const void *), char *(*toString)(const void *), void (*freeObject)(void *));
/* frees all memory associated with the arraylist */
void FreeArrayList(ArrayListPtr A);````