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
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.
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
Yes. You're freeing the same memory twice.
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.
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.