[Solved] How many bytes will be deallocated with free() after changing the pointer?

As the man page for free will tell you, any argument except a pointer returned from malloc has undefined behaviour: The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is … Read more

[Solved] Do I need to do a free on a struct? [closed]

you shall not free() instances that have not been allocated through malloc(), though it’s unclear how the instance has been allocated before being given to that function, though it’s unlikely it’s been malloc()ed as this is C++, which leads me to 2. ; you should not free() C++ objects, but instead call delete that will … Read more

[Solved] free() not working correctly with struct

It is always a good habit to set the pointer to NULL after freeing it. Since the memory you are accessing is still intact you are seeing right values for l->val. Node *l=malloc(sizeof(Node)); l->key=”foo”; l->val=”bar”; free(l); l = NULL; solved free() not working correctly with struct