What REALLY happens when you don't free after malloc?

前端 未结 18 769
南旧
南旧 2020-11-22 01:32

This has been something that has bothered me for ages now.

We are all taught in school (at least, I was) that you MUST free every pointer that is allocated. I\'m a

18条回答
  •  粉色の甜心
    2020-11-22 02:12

    I think that your two examples are actually only one: the free() should occur only at the end of the process, which as you point out is useless since the process is terminating.

    In you second example though, the only difference is that you allow an undefined number of malloc(), which could lead to running out of memory. The only way to handle the situation is to check the return code of malloc() and act accordingly.

提交回复
热议问题