malloc与realloc [英] malloc vs. realloc

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

问题描述

我有一个代码,其中我不知道数组可能包含多少元素,但我知道最大值。没有。它可能具有的价值。所以我这样做

malloc(MAXLEN),但每次我向这个数组添加一个新的

元素时我都可以使用realloc(...)。


现在我的问题是:每次运行realloc会慢慢减慢代码吗?b $ b多吗?我的项目关注高性能。

Pushkar Pradhan

解决方案

2003年11月24日星期一22 :06:19 -0600,Pushkar Pradhan写道:

我有一个代码,其中我不知道阵列可能包含多少元素,但是我知道最大值没有。它可能具有的价值。所以我这样做了/ malloc(MAXLEN),但每次我向这个数组添加一个新的
元素时我都可以使用realloc(...)。

现在我的问题是:每次进行realloc都会慢慢减慢代码吗?




是的,但这不是问题。不要为每个元素重新分配。每次

你用完了元素,使用realloc将数组的大小加倍。


" Sheldon Simms" < SH ********** @ yahoo.com>在消息中写道

news:pa **************************** @ yahoo.com ... < blockquote class =post_quotes>在星期一,2003年11月24日22:06:19 -0600,Pushkar Pradhan写道:

我有一个代码,其中我不知道知道阵列可能包含多少元素,但我知道最大值。没有。它可能具有的价值。所以我这样做了/ malloc(MAXLEN),但每次我向这个数组添加一个新的
元素时我都可以使用realloc(...)。

现在我的问题是:每次进行realloc都会慢慢减慢代码吗?



是的,但这不是问题。不要为每个元素重新分配。每次用完元素时,使用realloc将数组的大小加倍。



<不是主题,但可能到了这一点>

但是如果所有元素都必须在内存中很快或更晚,

为什么不在所有元素中使用malloc开始(可能是在初始假设大小不能保持的情况下使用realloc获得
)?

虽然稍后可能没有足够的空间来分配

更大的连续块,它可以在开头。如果它不是b $ b,为什么还要开始嘎吱嘎吱?所有后来的后续

分配(对于其他东西)可以更小并且适合。进入

可能碎片化的内存。

< /不在主题上,但可能到了这一点>


在星期一,2003年11月24日22:06:19 -0600,Pushkar Pradhan

< pu ***** @ gri.msstate.edu>在comp.lang.c中写道:

我有一个代码,其中我不知道数组可能包含多少元素,但我知道最大。没有。它可能具有的价值。所以我这样做了/ malloc(MAXLEN),但每次我向这个数组添加一个新的
元素时我都可以使用realloc(...)。

现在我的问题是:每次都会重新分配代码非常慢吗?我的项目关注高性能。
Pushkar Pradhan




C标准没有规定任何性能。很大程度上取决于你的特定编译器和操作系统上的b $ b,你可以在一个支持你系统的组中测试或询问。


很有可能至少有一些重新分配电话会在时间上花费很多。也许更好的内存分配

策略将是有序的。


如果你真的需要缩小内存块,你有可能

可以在添加所有元素之后而不是在每个元素之后执行吗?


如果不可能,您可以从
$开始分配b $ b最大尺寸的一部分,例如25%。如果你使用那个

,你可以分配另外25%,另一个和另一个。


其他可能性是选择一些可能的大小并且总是加倍<每当你需要增长块时,或者将
乘以1.5。


如果可能的话,你应该调查程序的典型需求。

元素的最大数量可能是1000,但也许大部分时间需要100或更少。


-

Jack Klein

主页: http:// JK- Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp .lang.c ++ http://www.parashift.com/c++-faq -lite /

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq


I''ve a code in which I don''t know how many elements an array may
contain, BUT I know the max. no. of values it may have. So I do this
malloc(MAXLEN), however I can use realloc(...) each time I add a new
element to this array.

Now my question is: will doing realloc everytime slow down the code very
much? My project is concerned about high performance.
Pushkar Pradhan

解决方案

On Mon, 24 Nov 2003 22:06:19 -0600, Pushkar Pradhan wrote:

I''ve a code in which I don''t know how many elements an array may
contain, BUT I know the max. no. of values it may have. So I do this
malloc(MAXLEN), however I can use realloc(...) each time I add a new
element to this array.

Now my question is: will doing realloc everytime slow down the code very
much?



Yes, but that''s not a problem. Don''t realloc for every element. Each time
you run out of elements, use realloc to double the size of the array.


"Sheldon Simms" <sh**********@yahoo.com> wrote in message
news:pa****************************@yahoo.com...

On Mon, 24 Nov 2003 22:06:19 -0600, Pushkar Pradhan wrote:

I''ve a code in which I don''t know how many elements an array may
contain, BUT I know the max. no. of values it may have. So I do this
malloc(MAXLEN), however I can use realloc(...) each time I add a new
element to this array.

Now my question is: will doing realloc everytime slow down the code very
much?



Yes, but that''s not a problem. Don''t realloc for every element. Each time
you run out of elements, use realloc to double the size of the array.


< not on-topic, but maybe to the point>
But if all elements will have to be in memory soon or later,
why not malloc for all of them at the beginning (possibly
with realloc after initial assumption about size won''t hold)?
While later on there may not be enough space to allocate
bigger continuous chunk, it can be on the beginning. If it is
not, why bother to start crunching? All later subsequent
allocations (for other stuff) could be smaller and "fit" into
possibly fragmented memory.
</ not on-topic, but maybe to the point>


On Mon, 24 Nov 2003 22:06:19 -0600, Pushkar Pradhan
<pu*****@gri.msstate.edu> wrote in comp.lang.c:

I''ve a code in which I don''t know how many elements an array may
contain, BUT I know the max. no. of values it may have. So I do this
malloc(MAXLEN), however I can use realloc(...) each time I add a new
element to this array.

Now my question is: will doing realloc everytime slow down the code very
much? My project is concerned about high performance.
Pushkar Pradhan



The C standard does not specify performance of anything. Much depends
on your particular compiler and operating system, which you can either
test or ask about in a group that supports your system.

There is a good chance that at least some reallocation calls will be
expensive in terms of time. Perhaps a better memory allocation
strategy would be in order.

If you really need to shrink the memory block, is it possible that you
can do it after all elements are added, instead of after each one?

If that is not possible, you could start out with an allocation for
some fraction of the maximum size, for example 25%. If you used that
up you could allocate another 25%, and another and another.

Other possibilities are to pick some likely size and always double it
or multiply it by 1.5 each time you need to grow the block.

You should investigate the typical needs of the program, if possible.
The maximum number of elements might be 1000, but perhaps most of the
time it will need 100 or less.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


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

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