realloc的变种 [英] realloc variants

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

问题描述

只是为了宅院的背景:它是关于一个字符串池,这意味着具有字符串键的哈希表(实际上是知道其长度的特殊字符串,但我想这个细节在这里无关紧要)。问题的关键是列表的数组(用作表桶)时,池需要增加的大小调整。但是 - 这是在内存中各个角落的核心,而不是为S. $ P $垫出detail--包含字符串实际上是在电池阵列单元,[1]。因此,我不需要再列出,他们只是过时的东西。所以:


  1. 有realloc的一个变种,零之类释放calloc的存储区域?
    我需要在这里因为项目不仅是三分球,但列出了头:问题是确保空列表/显示为NULL。否则,是最好的解决办法只是 memset的(P,大小,0);


  2. 有realloc的,如果说有没有足够的空间来对地方生长,而只是分配的alloc一样不复制的变种?这里的问题是,我不需要再数据,因为字符串需要根据新模重新分发列表。否则,什么是最好的选择吗?


    • 使用的realloc

    • (免费&安培;)使用页头

    • (免费&安培;)使用释放calloc


这是正确的在任何情况下:realloc的尝试的Alloc上进行更多的空间,否则,在其他地方分配默默副本?如果是的话,那么也许一个问题是,有(至少)三个用例需要--in两种情况下,有或没有上place--足够的空间,但没有选择,在所有单FUNC不同的操作:


  • 我需要这些数据和更多的惊喜多的地方(标准)。

  • 我需要更多的地方,但数据从现在开始都是垃圾。

  • 我需要更多的空间和面积被归零。

什么是我最好的选择吗?你觉得还有什么?我在哪里可以找到有关主题的更多reflexions或信息?

有没有为什么ALLOC拥有释放calloc和realloc不同的接口的原因是什么? (我的意思是指定的总规模VS单的大小和放大器;计数)

[1]点原本是做序集和放大器;地图;一个字符串池并不需要它,但不打扰。相反,它使code更清晰,提供的局部性。


解决方案

  

有realloc的一个变种,零的存储区域像
  释放calloc?我需要在这里因为项目不仅是三分球,但
  列出头:问题是确保空列表/显示为NULL。
  否则,是最好的解决办法只是memset的(P,大小,0);


如果你严格的编码标准,你坚持 memset的。至少这一个是容易解决。

如果你有一些特定于平台的余地,但也有一些有一个 recalloc 功能。


  

有realloc的不复制的一种变体,如果真有不
  足够的空间来成长的地方,而只是分配样页头?
  这里的问题是,我不需要再数据,因为串
  需要根据新的模数被重新分布在列表。否则,什么
  是最好的选择?


同样没有,很遗憾。

有对C1X 一个建议这增加了这两个功能(等等),但现在你不得不拥有的realloc 的当前行为。

Just to situate the context: it's about a string pool, meaning a hash table with string keys (actually special strings that know their length, but I guess this detail is irrelevant here). The point is the resizing of the array of lists (used as table buckets) when the pool needs to grow. But --this is the core detail-- cells containing string actually in an array of cells, instead of being spread out in all corners of memory [1]. Thus, I don't need the lists anymore, they're just outdated stuff. So:

  1. Is there a variant of realloc that "zeroes" the memory area like calloc? I need that here because the items are not only pointers, but lists heads: the issue is to ensure an empty list is/shows as NULL. Else, is the best solution just to memset(p, size, 0);

  2. Is there a variant of realloc that does not copy if ever there is not enough space to grow on place, but instead just allocates like alloc? The issue here is that I don't need the data anymore, since strings need to be re-distributed in lists according to new modulo. Else, what is the best choice?

    • use realloc
    • (free &) use alloc
    • (free &) use calloc

Is this right in any case: realloc tries to alloc more space on place, else allocates elsewhere and silently copies? If yes, then maybe a problem is there are (at least) three use cases requiring different actions --in both cases where there is or there is not enough space on place-- but a single func with no option at all:

  • I need more place for these data and more to come (standard).
  • I need more place, but the data are garbage from now on.
  • I need more space and the area to be "zeroed".

What is the best option for me? What do you think else? Where can I find more reflexions or info on the topic?

Is there a reason why alloc has a different interface from calloc and realloc? (I mean specifying total-size vs single-size & count)

[1] The point was originally to make ordered sets & maps; for a string pool it is not needed but does not bother. Instead, it makes code clearer and provides locality of reference.

解决方案

Is there a variant of realloc that "zeroes" the memory area like calloc? I need that here because the items are not only pointers, but lists heads: the issue is to ensure an empty list is/shows as NULL. Else, is the best solution just to memset(p, size, 0);

If you're coding strictly for the standard, you're stuck with memset. At least this one is easy to work around.

If you've got some platform-specific leeway, some of them do have a recalloc function.

Is there a variant of realloc that does not copy if ever there is not enough space to grow on place, but instead just allocates like alloc? The issue here is that I don't need the data anymore, since strings need to be re-distributed in lists according to new modulo. Else, what is the best choice?

Again no, unfortunately.

There is a proposal for C1X which adds both of these features (among others), but for now you're stuck with realloc's current behavior.

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

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