realloc的内部实现 [英] Internal implementation of realloc

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

问题描述

如何在内部实施realloc?如果没有足够的内存来分配,是否在其他地方分配了新内存,并且通过指针链接的

2区域OR是复制到的旧区域

哪里有足够的内存以适应新旧区域?在

第二种情况下,如果旧的

区域很大,这种方法效率不高吗?

How is realloc implemented internally ? If there is not enough memory
in place to allocate , is new memory allocated somewhere else and the
2 regions linked via a pointer , OR , is the old region copied to a
place where there is enough memory to fit both old and new region ? In
the second case , wouldn''t this method be inefficient if the old
region was large ?

推荐答案

Kislay写道:
Kislay wrote:

如何在内部实施realloc?
How is realloc implemented internally ?



适用于早期线程''printf'的相同答案适用。


-

Ian Collins。

The same answers that were given to the earlier thread ''printf'' apply.

--
Ian Collins.


Kislay写道:
Kislay wrote:

如何在内部实施realloc?如果没有足够的内存来分配,是否在其他地方分配了新内存,并且通过指针链接的

2区域OR是复制到的旧区域

哪里有足够的内存以适应新旧区域?
How is realloc implemented internally ? If there is not enough memory
in place to allocate , is new memory allocated somewhere else and the
2 regions linked via a pointer , OR , is the old region copied to a
place where there is enough memory to fit both old and new region ?



复制。

Copied.


在第二种情况下,如果旧的话,这种方法效率不高

区域很大?
In the second case , wouldn''t this method be inefficient if the old
region was large ?



可能是。

它可以将数组扩展为N个元素,

到O( N * N)操作。


将链表扩展到N个节点,

更可能是O(N)操作。
< br $> b $ b -

pete

It could be.
It could make expanding an array to N elements,
into an O(N*N) operation.

Expanding a linked list to N nodes,
would more likely be an O(N) operation.

--
pete




" Kislay" < ki *********** @ gmail.com在消息中写了

"Kislay" <ki***********@gmail.comwrote in message

如何在内部实施realloc?如果没有足够的内存来分配,是否在其他地方分配了新内存,并且通过指针链接的

2区域OR是复制到的旧区域

哪里有足够的内存以适应新旧区域?在

第二种情况下,如果旧的

区域很大,这种方法效率不高吗?
How is realloc implemented internally ? If there is not enough memory
in place to allocate , is new memory allocated somewhere else and the
2 regions linked via a pointer , OR , is the old region copied to a
place where there is enough memory to fit both old and new region ? In
the second case , wouldn''t this method be inefficient if the old
region was large ?



伪码


获得块大小

块后空格

if(blocksize + space< new blocksize)

调整块

其他

分配新块

复制数据

免费旧块


只需一个

getblocksize()函数就可以实现一个效率相当低的realloc()。如果你可以窥视malloc的内部结构(0

系统,你可以扩展块,如果可能的话,这样可以避免复制。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


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

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