Is this a double free in C?

后端 未结 5 1313
余生分开走
余生分开走 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.

提交回复
热议问题