does exit() free allocated memory on both _SUCCESS and _FAILURE

前端 未结 4 2493
旧时难觅i
旧时难觅i 2021-02-19 16:43

This a short snippet of code, with two calls to exit(3) in case of failure. Do these calls deallocate memory allocated by malloc? Google search once says it does, a

4条回答
  •  不知归路
    2021-02-19 17:27

    Just a note that at those 2 calls to exit you have - you have failed to allocate any memory, so freeing that pointer is going to be pretty pointless (and might crash, depending on how old your C runtime system is).

    So, no, you shouldn't free it, because it doesn't exist.

    I'd have said in the case of a fatal error like that, you probably don't want to bother freeing up memory.

    However, if your program exits normally, yes, you should try and free up all the memory you have allocated. That can be quite tricky sometimes.

提交回复
热议问题