malloc的内存分配 [英] Memory allocation by malloc

查看:55
本文介绍了malloc的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人请告诉我,malloc如何从堆中分配内存什么

数据结构用来这么做。

问候

Ardhendu

解决方案



" Ardhendu Nandan" < AR ************* @ rediffmail.com>在消息中写道

news:41 ************************** @ posting.google.c om ...


有人请告诉我,malloc如何从堆中分配内存
它正在使用什么数据结构。




查看Knuth:计算机编程的艺术,第二卷(IIRC)。


简而言之,没有*单*答案,这取决于实施。

有很多方法可以做到这一点,每个方法都有一个特定的专业版'' s和

con'的。


Ardhendu Nandan写道:

有人请告诉我,
malloc如何从堆中分配内存
&它使用什么数据结构。




免费存储空间由免费列表管理。

异想天开术语堆是旧的IBM程序员用来区分从堆栈中实现免费列表的一个双关语。

malloc在免费列表中搜索一个片段

的足够大的免费存储空间

以容纳所请求的分配

并将其从免费列表中删除。

免费将已解除分配的存储空间

重新插入免费列表,并将其与

任意相邻的免费存储片段合并。


电子。 Robert Tisdale写道:

Ardhendu Nandan写道:

有人请告诉我,
malloc如何分配内存堆
&它用来做什么数据结构。



免费存储由免费列表管理。
异想天开的术语堆。是旧的IBM程序员用来区分自由列表与堆栈的实现的一个双关语。
malloc在免费列表中搜索大型免费存储的片段足够
以适应所请求的分配
并将其从免费列表中删除。
将释放的存储空间插入自由列表中并将其与
任何相邻片段合并(s)免费存储。




术语堆。可能是也可能不是来自IBM的b $ b,可能是也可能不是双关语(如果是的话,它对我来说太微妙了)。至于其余的要点:


- 某些实现可能会使用免费列表。

管理动态内存。有些使用了几个这样的

列表。有些人使用树,位图或其他

数据结构,这些数据结构没有很好地描述为

" list。"


- 一些malloc()实现搜索免费的

存储空间。其他人只需准备就绪就可以了,而不需要任何搜索。


- 一些free()实现合并已发布的

与相邻的自由区域存储。其他人不会这样做,或者只在某些情况下这样做。


标准规定了内存管理

功能必须做什么,但不是他们如何做到这一点 - 这个

给予实施者在选择数据方面的宽广范围

结构和适合该平台的算法。

选择各不相同,而且比Tisdale先生的

回复建议更广泛。


-
呃********* @ sun.com


Hi,
Will someone please tell me, How malloc allocating memory from heap & what
data structure it is using to do so.
regards
Ardhendu

解决方案


"Ardhendu Nandan" <ar*************@rediffmail.com> wrote in message
news:41**************************@posting.google.c om...

Hi,
Will someone please tell me, How malloc allocating memory from heap & what data structure it is using to do so.



Check out Knuth: The art of computer programming, Vol II (IIRC).

In short, there is no *single* answer, it depends on the implementation.
There are many ways of doing this, each having a specific set of pro''s and
con''s.


Ardhendu Nandan wrote:

Will someone please tell me,
"How does malloc allocate memory from heap
& what data structure it is using to do so."



Free storage is managed by a "free list".
The whimsical term "heap" is a pun
which old IBM programmers used to distinguish
their implementation of a free list from a stack.
malloc searches the free list for a fragment
of free storage that is large enough
to accommodate the requested allocation
and removes it from the free list.
free inserts the deallocated storage
back into the free list merging it with
any adjacent fragment(s) of free storage.


E. Robert Tisdale wrote:

Ardhendu Nandan wrote:

Will someone please tell me,
"How does malloc allocate memory from heap
& what data structure it is using to do so."


Free storage is managed by a "free list".
The whimsical term "heap" is a pun
which old IBM programmers used to distinguish
their implementation of a free list from a stack.
malloc searches the free list for a fragment
of free storage that is large enough
to accommodate the requested allocation
and removes it from the free list.
free inserts the deallocated storage
back into the free list merging it with
any adjacent fragment(s) of free storage.



The term "heap" may or may not have originated with
IBM, and may or may not be a pun (if it is, it''s too
subtle for me). As for the remaining points:

- Some implementations may use a "free list" to
manage dynamic memory. Some use several such
lists. Some use trees, bit-maps, or other
data structures not well described by the term
"list."

- Some malloc() implementations search for free
storage. Others simply have it ready to dole
out, without any search required.

- Some free() implementations merge the released
storage with adjacent free areas. Others do
not, or do so only in some circumstances.

The Standard specifies what the memory management
functions must do, but not how they are to do it -- this
gives the implementor wide latitude in choosing data
structures and algorithms that suit the platform well.
The choices vary, and more widely than Mr. Tisdale''s
reply suggests.

--
Er*********@sun.com


这篇关于malloc的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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