Why doesn't free() zero out the memory prior to releasing it?

后端 未结 14 2948
长情又很酷
长情又很酷 2020-12-15 12:17

When we free() memory in C, why is that memory not filled with zero? Is there a good way to ensure this happens as a matter of course when calling free()<

14条回答
  •  心在旅途
    2020-12-15 12:41

    The original C philosophy was to have keep implicit effects to an absolute minimum. If a programmer wants a pointer zeroed after the memory pointed to is freed, that's what the programmer should write. Those of us who do often use a macro like this one:

    #define FREE(P) ((void)(free((P)), (P) = NULL))
    

    Of course if the expression passed to FREE has side effects, one has just opened a large can of worms...

提交回复
热议问题