freeing a null pointer

后端 未结 7 998
天涯浪人
天涯浪人 2020-12-09 14:52

What happens inside memory if we try to free a pointer which is pointing to NULL? Is that ever valid?

Why does it not show any warning/error messages?

相关标签:
7条回答
  • 2020-12-09 15:35

    Once upon a very long time ago, there were implementations of 'free()' that crashed when given a null pointer to free. This only applies to implementations that pre-date the C89 (C90) standard that have not been modified to deal with the problem since.

    In my experience, there are essentially none of those implementations left (and nor should there be), so it is now safe to free null pointers.

    If you have any requirements to port to extremely weird and ancient systems, then maybe you should still be cautious. On the other hand, if you had such systems to worry about, you'd probably know about the issue (and a whole raft of other issues) - or there'd be some communal knowledge around the code that indicates this.

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