Im working on a project in C and I am making a stack data structure. I want to know if I pop the first element if I can then free it just to avoid dealing with potential memory leaks if it doesn't get free'd later on.
struct node_s {
int a;
int b;
struct node_s *next;
}
struct list_s {
struct node_s *head;
size_t size;
}
typedef struct node_s *nodep_t;
typedef struct list_s *listp_t;
will something like this work?
struct node_s pop(listp_t list) {
struct node_s node = *(list->head);
nodep_t new_head = list->head->next;
free(list->head);
list->head = new_head;
return node;
}
If not I would just do it how I though originally and free the memory manually later on:
nodep_t pop(listp_t list) {
nodep_t node = list->head;
list->head = list->head->next;
return node;
}