3

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?

steveha
  • 74,789
  • 21
  • 92
  • 117
Ivan
  • 409
  • 4
  • 17

2 Answers2

2

NULL always has the same value: 0.

You can do what you want. The way to do it is to put the "next" pointer at the very beginning of the struct, so it is always in the same place for each struct.

If you have any more structure than a single "next" pointer, you should probably make a struct out of it all, and then put the struct at the beginning of each struct. For example, if you will make a doubly-linked list with both "prev" and "next" pointers, I suggest making a struct with the two pointers.

If each struct can simply be freed, you can just call the free() function on each pointer. If you sometimes need to run a cleanup function, you should make your universal linked-list freeing function take a pointer to a cleanup function, and call the cleanup on each struct as it is reached in the list.

steveha
  • 74,789
  • 21
  • 92
  • 117
  • Null is 0 only at compile time, it is implementation dependent at run time (AFAIK). All the `next` pointers are at the beginning of `struct`. – Ivan Mar 05 '13 at 22:25
  • Discussion of legal values of NULL: http://stackoverflow.com/questions/2599207/can-a-conforming-c-implementation-define-null-to-be-something-wacky But in my experience, I have never seen NULL be any value other than 0. This includes Windows, Mac, Linux, and embedded. And, there is a *huge* amount of code that does things like `while (ptr->next) { do_something(ptr); ptr = ptr->next }` and a non-zero NULL would break all that code, so I consider it extremely unlikely that any platform would not conform to the usual practice of making NULL be 0. – steveha Mar 05 '13 at 22:41
0

You need to traverse through the entire list while freeing the pointers one at a time.

krato
  • 1,226
  • 4
  • 14
  • 30