为什么不免费()零出内存之前,释放它? [英] Why doesn't free() zero out the memory prior to releasing it?

查看:113
本文介绍了为什么不免费()零出内存之前,释放它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们免费()内存在C,这是为什么不内存以0?有没有保证这个叫发生时作为理所当然的一个很好的方式免费()

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()?

我宁愿不要冒险留下释放回操作系统内存中的敏感数据...

I'd rather not risk leaving sensitive data in memory released back to the operating system...

推荐答案

释放它需要额外的时间归零时出了内存块。由于大多数的时候居然出现在它不需要它不是默认设置。

Zeroing out the memory block when freeing it will require extra time. Since most of time there's actually no need in it it is not done by default.

如果你真的需要(比如你使用的存储器,用于存储密码或加密密钥) - memset的()释放块之前调用。写一个工具函数链 memset的()免费()不是任何问题。

If you really need (say you used memory for storing a password or a cryptographic key) - call memset() before freeing the block. Writing an utility function that chains memset() and free() is not a problem either.

这篇关于为什么不免费()零出内存之前,释放它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆