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

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

    C was originally designed as a system implementation language, and so C operations are generally as fast and as close to the metal as is practical. One key point in the design philosophy is that you can take several fast operations and make them into one slower and safer operation, but you can't take slower and safer operations and make a faster one.

    If you want a zero-and-free function, you can write one, and use it instead of free(). If you're concerned with security, I'd recommend it.

提交回复
热议问题