简体   繁体   English

如果缩小分配的内存大小,还要检查realloc()吗?

[英]Also check realloc() if shrinking allocated size of memory?

When you call realloc() you should check whether the function failed before assigning the returned pointer to the pointer passed as a parameter to the function... 当调用realloc() ,应在将返回的指针分配给作为参数传递给函数的指针之前检查函数是否失败...

I've always followed this rule. 我一直遵循这个规则。

Now is it necessary to follow this rule when you know for sure the memory will be truncated and not increased? 现在,当您确定内存将被截断且不会增加时,是否有必要遵循此规则?

I've never ever seen it fail. 我从未见过失败。 Just wondered if I could save a couple instructions. 只是想知道我是否可以保存一些说明。

realloc may, at its discretion, copy the block to a new address regardless of whether the new size is larger or smaller. 无论新大小是大还是小, realloc可以自行决定将块复制到新地址。 This may be necessary if the malloc implementation requires a new allocation to "shrink" a memory block (eg if the new size requires placing the memory block in a different allocation pool). 如果malloc实现需要新分配以“缩小”存储块(例如,如果新大小需要将存储块放置在其他分配池中),则这可能是必要的。 This is noted in the glibc documentation : glibc文档中对此进行了说明

In several allocation implementations, making a block smaller sometimes necessitates copying it, so it can fail if no other space is available. 在几种分配实现中,有时缩小块有时需要对其进行复制,因此,如果没有其他可用空间,它可能会失败。

Therefore, you must always check the result of realloc , even when shrinking. 因此,即使收缩,也必须始终检查realloc的结果。 It is possible that realloc has failed to shrink the block because it cannot simultaneously allocate a new, smaller block. 由于重新分配无法同时分配一个较小的新块,因此realloc无法缩小该块。

Even if you realloc (read carefully realloc(3) and about Posix realloc please) to a smaller size, the underlying implementation is doing the equivalent of malloc (of the new smaller size), followed by a memcpy (from old to new zone), then free (of the old zone). 即使您将realloc (请仔细阅读realloc(3)和有关Posix realloc的内容 )的大小减小,其基础实现也等效于malloc (具有较小的新大小),然后执行memcpy (从旧区域到新区域) ,然后从旧区域中free Or it may do nothing... (eg because some crude malloc implementations maitain a limited set of sizes -like power of two or 3 times power of two-, and the old and new size requirements fits in the same size....) 否则它可能什么也不做...(例如,因为某些粗略的malloc实现保留有限的一组大小-例如2或3的2倍幂,并且新旧大小要求都适合相同的大小...。 )

That malloc can fail. malloc可能失败。 So realloc can still fail. 因此,重新realloc仍然可能失败。

Actually, I usually don't recommend using realloc for that reason: just do the malloc , memcpy , free yourself. 实际上,出于这种原因,我通常不建议使用realloc :只需执行mallocmemcpyfree自己即可。

Indeed, dynamic heap memory functions like malloc rarely fail. 实际上,像malloc这样的动态堆内存功能很少会失败。 But when they do, chaos may happen if you don't handle that. 但是,当他们这样做时,如果不处理,可能会发生混乱。 On Linux and some other Posix systems you could setrlimit(2) with RLIMIT_AS -eg using bash ulimit builtin- to lower the limits for testing purposes. 在Linux和其他一些POSIX系统,你可以了setrlimit(2)RLIMIT_AS -例如,使用bash ulimit builtin-降低用于测试目的的限制。

You might want to study the source code implementations of C memory management . 您可能需要研究C内存管理的源代码实现。 For example MUSL libc (for Linux) is very readable code. 例如, MUSL libc (对于Linux)是非常易读的代码。 On Linux, malloc is often built above mmap(2) (the C library may allocate a large chunk of memory using mmap then managing smaller used and freed memory zones inside it). 在Linux上, malloc通常建立在mmap(2)之上 C库可以使用mmap分配很大的内存,然后管理其中较小的已使用和已释放的内存区域)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM