Does every malloc call have to be freed

后端 未结 9 862
伪装坚强ぢ
伪装坚强ぢ 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
    2020-12-20 16:17

    Let's take those one point at a time...

    1. If you return a char * that you know was created with malloc, then yes, it is your responsibility to free that. You can do that with free(myCharPtr).

    2. The OS will claim the memory back, and it won't be lost forever, but there's technically no guarantee that it will be reclaimed right when the application dies. That just depends on the operating system.

提交回复
热议问题