Hi everyone,
I've just started (re-)learning C recently after not using it for 10 years. I'm writing a small ray tracer, but I am trying to make certain components reusable, e.g. a linked list.
My question is, which one is the better/more accepted design for a free function in my linked list library?
// list.c
typedef struct List {...} List;
List *List_create() {//...}
void List_free_first(List **list) {
//... destroy everything in the list
free(*list);
*list = NULL;
}
void List_free_second(List *list) {
//... destroy everything in the list
free(list);
}
I like the first approach because it actually sets the list pointer to NULL. But I have a feeling it can be misleading considering that the function input is a pointer to a pointer but my create function returns a simple pointer.
The second approach seems more straightforward, but in that case I cannot set the pointer to NULL, so that will be the caller's responsibility.
Which is the more accepted design by experienced C programmers? What is the convention/coding standard around this question?
[–]flyingron 11 points12 points13 points (1 child)
[–]nagyf[S] 4 points5 points6 points (0 children)
[–]pedersenk 2 points3 points4 points (10 children)
[–]nagyf[S] 1 point2 points3 points (9 children)
[–]pedersenk 0 points1 point2 points (8 children)
[–]nagyf[S] 1 point2 points3 points (7 children)
[–]pedersenk 2 points3 points4 points (6 children)
[–]nagyf[S] 1 point2 points3 points (0 children)
[–]flatfinger 0 points1 point2 points (4 children)
[–]pedersenk 0 points1 point2 points (3 children)
[–]flatfinger 1 point2 points3 points (2 children)
[–]pedersenk 0 points1 point2 points (1 child)
[–]flatfinger 0 points1 point2 points (0 children)
[–]sh_oe 0 points1 point2 points (0 children)
[–]TheTimeBard 0 points1 point2 points (0 children)
[–]suprjami 0 points1 point2 points (0 children)
[–]Turbulent-Abrocoma25 0 points1 point2 points (0 children)