11

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ash
  • 24,276
  • 34
  • 107
  • 152

4 Answers4

13

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 freed memory will result in double free memory corruption.

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • oh, that's very clear, thankyou. Is there a way to detect if the memory for a pointer has bee deallocated? – Ash May 18 '11 at 03:24
  • I guess this is why I have see some best practice documents sugerst that you should always manually set your pointers to = NULL after you call `free(pointer)`. – Ash May 18 '11 at 03:26
  • @Ash: have a look at this post: http://stackoverflow.com/questions/4143382/how-to-check-deallocation-of-memory – phoxis May 18 '11 at 03:27
  • Definitely you should assign unused pointer with a NULL. This will help you identify which one is unused with `if - else` – phoxis May 18 '11 at 03:27
  • @phoxis: I disagree: this will prevent you from realizing that a pointer has been free'd multiple times, which is almost always a bug. – titaniumdecoy May 18 '11 at 03:30
  • depends on what you are implementing. If you are implementing a linked list, then assigning the last pointer to NULL would be best. And for determining for multiple free, yes that is also a point. – phoxis May 18 '11 at 03:32
4
  1. It will not make your pointer NULL.
  2. It will free the memory pointed by the pointer, leaving the pointer set to an unallocated segment of memory.
  3. If you don't use malloc or calloc between the calls it will give you a Segmentation Fault.
  4. "Best practice is that a pointer passes out of scope immediately after being freed." means that the pointer should stay on the stack so that it should not be set NULL explicitly because it will eventually go out of scope and be freed.
Radu Stoenescu
  • 3,187
  • 9
  • 28
  • 45
3

Only if you consider destroying your heap "harm". free() will not make your pointer null.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • So, how can I test to see if a pointer has been properly freed? – Ash May 18 '11 at 03:09
  • The call will always be successful, unless there's nothing to be freed , in which case it will casue a Segmentation Fault. – Radu Stoenescu May 18 '11 at 03:15
  • 2
    The wikipedia article I linked has a fairly terse but wise answer to this: "Best practice is that a pointer passes out of scope immediately after being freed." – Rooke May 18 '11 at 03:19
3

Without repeating the other answers, it is incumbent on you to null pointers once you have called free(). Calling free() twice on the same allocation will result in heap corruption.

SomeTypePtr my_type;
my_type = malloc(sizeof(someType));

/* do stuff */

free(my_type);
my_type = 0; // Throw away the pointer to released memory, so cannot either free it or use it in any way.

/* do a bunch of more stuff */

free(my_type); // OK now - calling free(0) is safe.
Keith
  • 6,756
  • 19
  • 23