checking for NULL before calling free

前端 未结 8 623
终归单人心
终归单人心 2020-12-03 13:45

Many C code freeing pointers calls:

if (p)
  free(p);

But why? I thought C standard say the free function doesn\'t do anything

相关标签:
8条回答
  • 2020-12-03 14:20

    there can be a custom implementation of free() in mobile environment. In that case free(0) can cause a problem. (yeah, bad implementation)

    0 讨论(0)
  • 2020-12-03 14:21

    As I understand, the no-op on NULL was not always there.

    In the bad old days of C (back around 1986, on a pre-ANSI standard cc compiler) free(NULL) would dump core. So most devs tested for NULL/0 before calling free.

    The world has come a long way, and it appears that we don't need to do the test anymore. But old habits die hard;)

    http://discuss.joelonsoftware.com/default.asp?design.4.194233.15

    0 讨论(0)
提交回复
热议问题