I would to implement the stack using linked-list. Moreover, I want to use something like templates.
Thus I got such structures:
- node
#define node(T) \
struct node_##T { \
T value; \
struct node_##T *next; \
}
- 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?