0
typedef char *string;
struct node_t
{
    struct node_t *prev;
    string data;
    struct node_t *next;
};
typedef struct node_t *node;

struct list_t{
    struct node_t *head;
    struct node_t *tail;
};

typedef struct list_t *list;
void list_destroy (list l){
    node next = NULL;
        if (l != NULL) {
            node tmp = l->head;
            while (tmp!=NULL) {
                next = tmp->next;
                free(tmp->data);
                free(tmp);
                tmp = next;
            }
            free(l);
        }
};

I am trying to write function to free double linked list, when I am free char* type data, why it still have '\0' left? how can I free it completely?

Snowden
  • 11
  • 1
  • You are not malloc'ing anything, so you don't need to free anything. If you want to empty a string, you should just use `memset`. – Nastor Jul 21 '21 at 09:16
  • I may forget to add string type define – Snowden Jul 21 '21 at 09:20
  • I still don't see anything dynamically allocated. If you declare a _string literal_, it is a constant, so you can't change it. If you instead use malloc in order to create a string, you can free it from memory, but it doesn't mean that the string is going to be an empty string. – Nastor Jul 21 '21 at 09:20
  • 1
    Please show a [mcve] – Jabberwocky Jul 21 '21 at 09:23
  • I cannot show more code because limitation, but as you can see, `string` is type as char pointer, so as to be memory allocated – Snowden Jul 21 '21 at 09:25
  • 1
    What do you mean by "_it still have '\0' left_"? – the busybee Jul 21 '21 at 09:35
  • 4
    Unrelated, this: `typedef char *string;` and this: `typedef struct node_t *node;` et'al are *completely* unhelpful. There are two legitimate cases where hiding pointer types in typedef aliases is sensible (callback function prototyping, and blackbox "handle" APIs) and this is *neither*. It makes your code *harder* to manage; not easier. Embrace your C-ness. Seeing a `*` screams "pointer action going on now/soon". Don't fight that; hug it. Regarding your question, nobody said freeing a pointer magically cleared the memory returned to the runtime for reuse, which is what you seem to be thinking. – WhozCraig Jul 21 '21 at 09:46
  • Where in your code did you take that screenshot? You must be aware that after passing a pointer to `free` function you are no longer allowed to dereference it. The memory location is not your's anymore. – Gerhardh Jul 21 '21 at 13:09
  • See [Is it a good idea to typedef pointers?](https://stackoverflow.com/q/750178/15168) — TL;DR, the answer is no with a possible exception for function pointers (and another for completely opaque types, but even that is dubious). – Jonathan Leffler Jul 21 '21 at 17:06

0 Answers0