Is this a double free in C?

走远了吗. 提交于 2019-12-01 07:03:35

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.

Yes. You're freeing the same memory twice.

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.

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 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!