C语言中关于free()的问题 [英] question on free() in C language

查看:81
本文介绍了C语言中关于free()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
free和malloc如何在C中工作?

Possible Duplicate:
How do free and malloc work in C?

free如何知道在程序中被调用时要释放多少字节的内存?

How does free know how many bytes of memory to be free'd when called in a program?

推荐答案

这是特定于实现的,但是调用malloc时,分配的内存大小保留在某个位置(通常与指针本身偏移).调用free时,它将使用该存储的大小.

This is implementation specific, but when malloc is called, the size of the allocated memory is kept somewhere (usually offset from the pointer itself). When free is called, it will use that stored size.

这就是为什么您只应该在malloc返回的指针上调用free的原因.

This is exactly why you should only ever call free on a pointer that was returned by malloc.

这篇关于C语言中关于free()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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