I have a bunch of structures that all look like
typedef struct {
A[1..100] *next; // this is not an array, just indicating A1 or A2 or A3 and so on
//other stuff that varies from struct to struct
} A[1..100] // A1, A2, and so on
I generate a few linked lists of different same type structures. Somewhere in a function, I allocate the memory with something like
A55 *struct_list;
A55 *next_in_list;
struct_list = (A55 *)malloc(sizeof(A55));
(*struct_list).next = NULL;
//some loop
next_in_list = (A55 *)malloc(sizeof(A55));
(*next_in_list).next = struct_list;
struct_list = next_in_list;
At the end of the loop, struct_list
is a pointer to the end of the linked list.
I would like to have a single function that would free any list regardless of the structure that populates it. I feel the following might work, but I need something which will not break any rules and might be implementation safe:
void freeStruct(*void start){
void ** current, * next;
current = (void **) start;
do{
next = *current;
free(current);
current = (void **) next;
}while(current != NULL)
}
My question is whether NULL has the same numerical value for all pointers to all types, including struct
. And, is there a better way of doing this without having to copy the same function 100 times for the different struct
definitions?