Is there any harm in calling 'free' for the same pointer twice in a C program?

c, memory-management

Solution

Deallocating a memory area with `free` does not make the contents of the pointer NULL. Suppose that you have `int *a = malloc (sizeof (int))` and `a` has `0xdeadbeef` and you execute `free (a)` then after execution `a` still contains `0xdeadbeef` but after the `free` call this memory address is no more reserved for you. Something like you have rented a flat with `malloc` used for some time, returned the flat by `free` then you might have a duplicate key for the flat, but it is not reserved for you.

Doing a `free` on an already `free`d memory will result in double free memory corruption.

Problem

If I have a c program, like: ``` SomeTypePtr my_type; my_type = malloc(sizeof(someType)); /* do stuff */ free(my_type); /* do a bunch of more stuff */ free(my_type); ``` Does the calling of 'free' for my_type do any harm? After I call free(my_type), does the pointer become a null pointer once again?

Original source

Related problems