Does every malloc call have to be freed

后端 未结 9 870
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 16:04

From what I understand because malloc dynamically assigns mem , you need to free that mem so that it can be used again.

  1. What happens if you return a char* that
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 16:13

    Yes, every call to malloc() has to be matched with a call to free().

    To answer your specific questions:

    1. You have to explicitly document your API telling the user whether the returned pointer has to be free()'d
    2. The OS will free all memory allocated to the process.

提交回复
热议问题