Using pointer after free()

后端 未结 5 1836
抹茶落季
抹茶落季 2020-11-27 22:54

During my tests, I have found that it is possible to use pointer after free(). I have the following code:

typedef struct{

    int module_id;
    int adc_id;         


        
5条回答
  •  眼角桃花
    2020-11-27 22:58

    When you call free(phead), the memory phead points to is being freed, but the value of phead stays untouched, making the phead a dangling pointer. Accessing the memory that has already been freed produces undefined behavior.

    It is a good practice to assign NULL to the pointer once you free the memory it points to:

    free(phead);
    phead = NULL;
    

提交回复
热议问题