Free structure and the heap

前端 未结 3 1234
名媛妹妹
名媛妹妹 2021-01-27 11:44

If we have a structure that has pointers to malloced memory and then if we free that structure, is the memory referenced by that structure also freed? or do we have to free it m

相关标签:
3条回答
  • 2021-01-27 12:02

    Its basically equals to number of malloc()s performed to allocate the memory. If you allocate memory dynamically using malloc() for both struct pointers fomResult & fomResult you should use separate two free() functions to free there memory along with freeing MResult.

    0 讨论(0)
  • 2021-01-27 12:04

    You'll have to free memory pointed by fomResult and homResult before you free your MResult, otherwise you'll end up with memory leak.

    Of course if you have other pointer to those memory blocks you can use them to release the memory.

    0 讨论(0)
  • 2021-01-27 12:25

    is the memory referenced by that structure also freed? or do we have to free it manually?

    The latter.

    Each memory block dynamically allocated needs a separate call to free().

    0 讨论(0)
提交回复
热议问题