free()如何知道? [英] How does free() knows?

查看:59
本文介绍了free()如何知道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道free()函数如何知道有多少内存被释放,因为我们只将指向已分配内存的指针作为

free的参数( )。当我们使用malloc()时,系统是否使用内部变量存储已分配的内存



Plz帮助......

I wish to know how the free()function knows how much memory to be
freed as we only give pointer to allocated memory as an argument to
free(). Does system use an internal variable to store allocated memory
when we use malloc().
Plz help......

推荐答案

Suyog_Linux< su ****** @ yahoo.com>在comp.lang.c上潦草地写下以下

Suyog_Linux <su******@yahoo.com> scribbled the following
on comp.lang.c:
我想知道free()函数如何知道释放了多少内存,因为我们只有将分配的内存指针作为
free()的参数。当我们使用malloc()时,系统是否使用内部变量来存储分配的内存。
Plz帮助......
I wish to know how the free()function knows how much memory to be
freed as we only give pointer to allocated memory as an argument to
free(). Does system use an internal variable to store allocated memory
when we use malloc().
Plz help......




它完全取决于实施。内部变量是一个

方式,但可能还有其他变量。该标准并没有强制要求任何具体的方式。


-

/ - Joona Palaste(pa **** *@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ----------- ----------规则! -------- /

从互联网上复制音乐的问题就像一把两把剑。

- 芬兰说唱艺术家Ezkimo



It depends entirely on the implementation. An internal variable is one
way, but there might be others. The standard does not mandate any
specific way.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo


" Suyog_Linux" <苏****** @ yahoo.com>在留言中写道

news:df ************************** @ posting.google.c om ...
"Suyog_Linux" <su******@yahoo.com> wrote in message
news:df**************************@posting.google.c om...
我想知道free()函数如何知道要释放多少内存,因为我们只将指向已分配内存的指针作为
free()的参数。当我们使用malloc()时,系统是否使用内部变量来存储分配的内存。
I wish to know how the free()function knows how much memory to be
freed as we only give pointer to allocated memory as an argument to
free(). Does system use an internal variable to store allocated memory
when we use malloc().




很可能是的,尽管用户没有可移植的方式'程序到

得到这个值。


我想它也可能不会保持长度明确但是

可能会保留一个指向下一个块的指针,或者只是保留各种大小的
块列表,并获得malloc从列表中分发一个块

下一个更大的区块...或其他方案。


鉴于系统几乎肯定知道该块有多大我总是很少

a little很惊讶没有getallocsize(void *)或一些这样的

函数来获取块中分配的空间量,因为它对某些应用程序来说相当有用。



Most likely yes, although there is no portable way for users'' programs to
get at this value.

I guess it''s also possible that it might not keep the length explicity but
might instead keep a pointer to the next block, or maybe just keeps lists of
blocks of various sizes and get malloc to hand out a block from the list
with the next larger block... Or other schemes.

Given that the system almost certainly knows how big the block is I''m always
a little suprised that there is not getallocsize(void*) or some such
function to get the amount of allocated space in a block as it would be
fairly useful for some applications.


su******@yahoo.com (Suyog_Linux)写道:

#I希望知道free()函数如何知道要释放多少内存

#释放,因为我们只将指向已分配内存的指针作为

#free()的参数。当我们使用malloc()时,系统是否使用内部变量来存储已分配的内存

#。


允许实现任何正确实现接口的魔法。


随着数TB的VM和64位地址变得可用,许多程序的可接受的malloc

实现是malloc总是分配新的内存

和免费什么都不做,但马上回来。这样的实现并不需要在任何地方存储块大小。


-

SM Ryan http://www.rawbw.com/~wyrmwif/

谁领导这些暴民?
su******@yahoo.com (Suyog_Linux) wrote:
# I wish to know how the free()function knows how much memory to be
# freed as we only give pointer to allocated memory as an argument to
# free(). Does system use an internal variable to store allocated memory
# when we use malloc().

The implementation is permitted any magic that correctly implements the interface.

With terabytes of VM and 64 bit addresses becoming available, an acceptable malloc
implementation for many programs is that malloc always allocates new memory
and free doesn''t do anything but immediately return. Such an implementation doesn''t
need to store block sizes anywhere.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who''s leading this mob?


这篇关于free()如何知道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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