Freeing malloced structure in a function

后端 未结 4 1446
一生所求
一生所求 2020-12-21 03:42

I\'m creating a source files containing buffer functionality that I want to use for my other library that I\'m creating.

It is working correctly but I\'m having tro

4条回答
  •  一个人的身影
    2020-12-21 04:23

    free() call will just mark the memory in heap as available for use. So you still have the pointer pointing to this memory location but it's not available anymore for you. Thus, the next call to malloc() is likely to assign this memory to the new reservation.

    To void this situations normally once you free() the memory allocated to a pointer you should set it to NULL. De-referencing NULL is UB also but at least when debugging you can see tha pointer should not be used because it's not pointing to a valid memory address.

提交回复
热议问题