What does “double free” mean?

前端 未结 4 2077
傲寒
傲寒 2020-12-09 02:21

As the title suggests I am new to C and have a mid-term coming up shortly. I am revising from past papers currently and a recurring theme is double free problem. I understan

4条回答
  •  不知归路
    2020-12-09 03:05

    This question has been well answered, but I add a late answer due to a "duplicate question" link, which asked "how to avoid it?"

    One line is added to the example code posted.

    char* ptr = malloc(sizeof(char));
    
    *ptr = 'a';
    free(ptr);
    ptr = NULL;         // add this
    free(ptr);
    

    Function free does nothing with a NULL pointer.

提交回复
热议问题