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

后端 未结 14 2904
长情又很酷
长情又很酷 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:47

    Once you free memory using free(), the value & the memory allocated at that particular address gets deleted (freed) but the pointer still points to that address. If you try to de-reference that pointer you will get Segmentation fault or Bus error. So, its safe to assign NULL value to the pointer once the memory pointed by the pointer is freed. You may refer < Setting variable to NULL after free >

提交回复
热议问题