Does every malloc call have to be freed

后端 未结 9 858
伪装坚强ぢ
伪装坚强ぢ 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:26

    1) The same way you'd free the memory normally, i.e.

    p = func();
    //...
    free(p);
    

    The trick is in making sure that you always do it...

    2) Generally speaking, yes. But you should still free any memory you use as good practice. Not spending the time to figure out where to free the memory is just being lazy.

提交回复
热议问题