Does every malloc call have to be freed

后端 未结 9 859
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 16:08

    I wouldn't go so far as to say every malloc must be freed, but I would say that, no matter how long a program runs, there must be a bounded number of allocations (and total size) that won't be freed. The number need not be a static constant, but it must be specifiable in terms of something else (e.g. this program processes widgets; it will allocate one 64-byte struct for each quizzix in the largest widget). One may not know beforehand the size of the largest widget, but if e.g. one knows that the temporary storage required to process a widget is proportional to the square of its size, one might safely infer that the largest widget will be small enough that the total amount of memory stranded will be pretty slight.

提交回复
热议问题