Why does free() only set the 8 first bytes to zero?

狂风中的少年 提交于 2019-12-09 06:50:28

free() is not required to clear memory, and really it shouldn't because doing so would take time and overwrite cached data for no universally required benefit.

It's certainly allowed to use the internal space for its own purposes. What you are seeing may be just a side effect of the allocator keeping track of free memory.

A better question would be; why do you think it should do anything else? Where did you find documentation regarding the mandated implementation of free? It doesn't exist, so it makes no sense to ask such a question.

free simply has to mark that memory as freed. What happens to that block of memory after calling free is unspecified.


As an aside, this is UB:

printf ("Content ptr    : %s\n", ptr);
printf ("Content ptr+8 : %s\n", ptr+8);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!