Home:ALL Converter>freeing a linked_list's nodes, by freeing whole object

freeing a linked_list's nodes, by freeing whole object

Ask Time:2019-12-18T22:15:59         Author:JuiceFV

Json Formatter

I would to implement the stack using linked-list. Moreover, I want to use something like templates. Thus I got such structures:

  1. node
#define node(T)            \
  struct node_##T {        \
    T value;               \
    struct node_##T *next; \
  }
  1. stack
#define stack(T)           \
  struct stack_##T {       \
    size_t size;           \
    node(T) * begin, *end; \
  }

At the end I want to free the stack. Always I did it using while/recursive. Somthing like that:

while (begin != NULL)
{
    temp = begin;
    begin = begin->next;
    free(temp);
}

In my case the usage of this method is more compicated rather than usally. Consequently, I just freeing the object.

#include "stack.h"
typedef char* string;
int main() {
  stack(string)* a;
  stack_constructor(string, a);
  stack_resize(a, 5);
  free(a);
  return (0);
}

The debug shows that all is fine. I have set two break-points before "free(a)" and after "free(a)". before freeing after freeing But I have some doubts about these. I think that we're deleting the memory which we have allocated for the pointer, but the values still in memory. Am I mistakning?

Author:JuiceFV,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59393964/freeing-a-linked-lists-nodes-by-freeing-whole-object
yy