tags:

views:

184

answers:

6

Normally, if a pointer is freed twice, it's a double free. For example,

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

The above code is considered as double free. Is the following considered as double free as well?

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

Thank you.

+10  A: 

Yes. You're freeing the same memory twice.

Thom Smith
rlb.usa
A: 

Yep. A double free is when you attempt to free a memory block that has already been freed. Both ptr and ptr1 are pointing to the same memory block, so the second call to free is attempting to free a memory block that is already freed.

torak
+8  A: 

Yes. The library doesn't care what name you gave a varaible in your source code (it's long gone by the time the code is executed). All that matters is the value, and in this case the values passed to free() would be the same.

James Curran
A: 

Yes as the pointers both point to the same address and so pass the same address to free.

You can show what the value of the pointer is by printing it

printf( "%p", ptr);

or look at it in a debugger

Mark
A: 

Yes this is a double free (and a very bad thing to do). The ptr1 is a pointer to the memory allocated by the malloc. It is the same location that ptr was pointing to. By freeing ptr and ptr1 you are freeing the same memory twice.

Gangadhar
A: 

See Here Double Free

deepseefan