top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can a pointer be freed more than once in C?

+2 votes
1,011 views

What happens if do so? Or can a pointer be freed twice in C?

posted Jan 22, 2015 by Balu

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

+3 votes

1st scenario: After freeing a pointer in a C program, freed memory might be reallocated by some other or same program luckily. In this scenario, freeing the same pointer twice won’t cause any issue.

2nd scenario: We can free a pointer. Then, we can allocate memory for same pointer variable. Then, we can use it and free it again. This is also not an issue.

3rd scenario: If we free the same pointer second time without reallocating memory to that pointer, then what happens? As per ANSI/ISO C standard, this is undefined behavior. This undefined behavior may cause anything to the program that we do not expect to happen.

answer Jan 23, 2015 by Shivaranjini
+2 votes

It can corrupt the heap.

answer Jan 22, 2015 by Aditya
+2 votes

Deallocating a memory area with free does not make the contents of the pointer NULL. Suppose that you have int *p = malloc (sizeof (int)) and a has 0xabcdef and you execute free(p) then after execution p still contains 0xabcdef but after the free call this memory address is no more reserved for you. Doing a free on an already freed memory will result in double free memory corruption.

answer Jan 22, 2015 by Salil Agrawal
+2 votes

In addition to Salil Agrawal's ans, I would suggest to use "ptr = NULL" after calling the free().

So even if you call 2nd time free() on already free() memory. It won't crash due to double free or corruption. If you assing NULL to that pointer after calling free().

answer Jan 23, 2015 by Arshad Khan
Similar Questions
+3 votes

Called by reference is a mechanism where we can bring the changes to a variable done in the called function to calling function which can be done via passing pointers in C. But most of the books are referring that C does not support pass by reference method.

Can someone explain why?

+3 votes

How we can force a function to be called only once. I can do this using some static variable or using boost library, but is there some other way to do this as I don't want to use any of the earlier way. Function is a non member function of any class.

0 votes

I'm somewhat confused working with @staticmethods. My logger and configuration methods are called n times, but I have only one call.
n is number of classes which import the loger and configuration class in the subfolder mymodule. What might be my mistake mistake?

### __init__.py ###

from mymodule.MyLogger import MyLogger
from mymodule.MyConfig import MyConfig

##### my_test.py ##########
from mymodule import MyConfig,MyLogger

#Both methods are static
key,logfile,loglevel = MyConfig().get_config('Logging')
log = MyLogger.set_logger(key,logfile,loglevel)
log.critical(time.time())

#Output
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - **********.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - **********.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - **********.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - **********.19
...