lillq
lillq

Reputation: 15389

What happens when you try to free() already freed memory in c?

For example:

char * myString = malloc(sizeof(char)*STRING_BUFFER_SIZE);
free(myString);
free(myString);

Are there any adverse side effects of doing this?

Upvotes: 39

Views: 23013

Answers (14)

Jens
Jens

Reputation: 72639

It (potentially) makes demons fly out of your nose.

Upvotes: 7

pinpinokio
pinpinokio

Reputation: 515

Another interesting situation:

char * myString = malloc(sizeof(char)*STRING_BUFFER_SIZE);
char * yourString = myString;

if (myString)
{
    free(myString);
    myString = NULL;
}
// Now this one is safe, because we keep to the rule for 
// setting pointers to NULL after deletion ...
if (myString)
{
    free(myString);
    myString = NULL;
}

// But what about this one:
if (yourString)
{
    free(yourString);
    yourString = NULL;
}

//?!? :)

Upvotes: -1

Martin Beckett
Martin Beckett

Reputation: 96109

Always set a pointer to NULL after freeing it. It is safe to attempt to free a null pointer.

It's worth writing your own free wrapper to do this automatically.

Upvotes: 3

lillq
lillq

Reputation: 15389

Answer summary:

Yes, bad things can and probably will happen.

To prevent this do:

free(myString);
myString = NULL;

Note that all references to the memory must be set to NULL if others were created.

Also, calling free() with a NULL results in no action. For more info see: man free

Upvotes: 9

mbac32768
mbac32768

Reputation: 11623

The admittedly strange macro below is a useful drop-in replacement for wiping out a few classes of security vulnerabilities as well as aid debugging since accesses to free()'d regions are more likely to segfault instead of silently corrupting memory.

#define my_free(x) do { free(x); x = NULL; } while (0)

The do-while loop is to help surrounding code more easily digest the multiple-statements. e.g. if (done) my_free(x);

Upvotes: 1

Chris Conway
Chris Conway

Reputation: 55989

Here's the chapter and verse.

If the argument [to the free function] does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined. (ISO 9899:1999 - Programming languages — C, Section 7.20.3.2)

Upvotes: 42

Christian.K
Christian.K

Reputation: 49240

In short: "Undefined Behavior".

(Now, what that can include and why that is the case the others have already said. I just though it was worth mentioning the term here as it is quite common).

Upvotes: 1

luke
luke

Reputation: 37443

It may crash your program, corrupt memory, or have other more subtle negative effects. After you delete memory, it is a good idea to set it to NULL (0). Trying to free a null pointer does nothing, and is guaranteed to be safe. The same holds true for delete in c++.

Upvotes: 1

BCS
BCS

Reputation: 78516

Bad Things (TM)

Really, I think it's undefined so anything at all including playing "Global Thermonuclear War" with NORAD's mainframe

Upvotes: 4

Armin Ronacher
Armin Ronacher

Reputation: 32533

Not so clever. Google for double free vulnerabilities. Set your pointer to NULL after freeing to avoid such bugs.

Upvotes: 6

mbac32768
mbac32768

Reputation: 11623

One of nothing, silent memory corruption, or segmentation fault.

Upvotes: 20

Khoth
Khoth

Reputation: 13328

Don't do that. If the memory that got freed is re-allocated to something else between the calls to free, then things will get messed up.

Upvotes: 2

John Millikin
John Millikin

Reputation: 200766

Depending on which system you run it on, nothing will happen, the program will crash, memory will be corrupted, or any other number of interesting effects.

Upvotes: 5

jbleners
jbleners

Reputation: 1043

Yes, you can get a double free error that causes your program to crash. It has to do with malloc's internal data structures to keep track of allocated memory.

Upvotes: 15

Related Questions