简体   繁体   中英

Freeing pointers in structs pointing to other structs

Let's say I have some data structure in which structs have pointers to other structs, such as a linked list. Each element in the list would have a pointer to another element in the list

struct node{
  char* data;
  node* next;
};

When freeing the memory allocated to this structure, I am aware I need to specifically free data first, as I have to allocate memory for it specifically. But what about next? It points to another node, and I believe freeing it would free the actual node next is pointing to.

Is it enough to just free the entire node when cleaning up, or is there any way to free the pointer without freeing the struct it is pointing to?

It depends how you are building the structure up in memory. Any malloc must be balanced with free . Once the pointed-to next node is freed, it is no longer valid.

// create root
node* n1 = calloc(1, sizeof(node));

// create/link another node
n1->next = calloc(1, sizeof(node));

// destroy/unlink 2nd node
free(n1->next);
n1->next = NULL;

// destroy 1st node
free(n1);

There's no thing like freeing a pointer , you can only free memory a pointer points to -- so if you want to delete a single element of a linked list, you have to update the next pointer of the previous item so it points to the next of the element you want to delete (which may be NULL ) before deleting it.

void deleteNode(struct node **list, struct node *element)
{
    /* first node? */
    if (element == *list)
    {
        *list = element->next;
        free(element->data);
        free(element);
        /* now list points to the second element, if any */
    }
    else
    {
        /* find previous node */
        struct node *p = *list;
        while (p->next != element) p = p->next;

        /* adjust next pointer */
        p->next = element->next;
        free(element->data);
        free(element);
    }
}

Note that using a singly-linked list is not suitable for big lists with random deletes because you have to search the list from the beginning each time.

For deleting a whole linked list, there's an iterative and a recursive approach, but I'm not showing code here because as I understood your question, this is not what you wanted to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM