Is it double free in C?

Usually, if the pointer is freed twice, it is double free. For instance,

char *ptr; ptr=malloc(5 * sizeof(*ptr)); free(ptr); free(ptr); 

The above code is considered double. Are the following, also considered double, free?

 char *ptr; char *ptr1; ptr=malloc(5 * sizeof(*ptr)); ptr1=ptr; free(ptr); free(ptr1); 

Thanks.

+4
source share
5 answers

Yes. The library doesnโ€™t care what name you gave varaible in your source code (it took a long time to run the code). All that matters is the value, in which case the values โ€‹โ€‹passed to free() will be the same.

+10
source

Yes. You free the same memory twice.

+10
source

Yeah. Double freedom is an attempt to free an already freed block of memory. Both ptr and ptr1 point to the same memory block, so the second call to free tries to free the already freed memory block.

0
source

Yes, because pointers point to the same address and therefore pass the same address to a free one.

You can show how important the pointer is by typing it

 printf( "%p", ptr); 

or see it in the debugger

0
source

Yes, this is double free (and a very bad thing). Ptr1 is a pointer to the memory allocated by malloc. This is the same place ptr was pointing to. By freeing ptr and ptr1, you free the same memory twice.

0
source

All Articles