Is this a double free in C?

后端 未结 5 1311
余生分开走
余生分开走 2021-01-13 18:27

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

相关标签:
5条回答
  • 2021-01-13 18:32

    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.

    0 讨论(0)
  • 2021-01-13 18:42

    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

    0 讨论(0)
  • 2021-01-13 18:47

    Yes. You're freeing the same memory twice.

    0 讨论(0)
  • 2021-01-13 18:58

    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.

    0 讨论(0)
  • 2021-01-13 18:59

    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.

    0 讨论(0)
提交回复
热议问题