TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

A Story Of realloc (And Laziness)

130 点作者 janerik大约 11 年前

8 条评论

optimiz3大约 11 年前
Code in the article for realloc is dangerous and wrong:<p><pre><code> void *realloc(void *ptr, size_t size) { void *nptr = malloc(size); if (nptr == NULL) { free(ptr); return NULL; } memcpy(nptr, ptr, size); &#x2F;&#x2F; KABOOM free(ptr); return nptr; } </code></pre> Line marked KABOOM copies $DEST_BYTE_COUNT, rather than $SOURCE_BYTE_COUNT.<p>Say you want to realloc a 1 byte buffer to a 4 byte buffer - you just copied 4 bytes from a 1 byte buffer which means you&#x27;re reading 3 bytes from 0xDEADBEEF&#x2F;0xBADF000D&#x2F;segfault land.<p>EDIT: Also, this is why the ENTIRE PREMISE of implementing your own reallocator speced to just the realloc prototype doesn&#x27;t make much sense. You simply don&#x27;t know the size of the original data with just a C heap pointer as this is not standardized AFAIK.
评论 #7542173 未加载
评论 #7542258 未加载
评论 #7544045 未加载
评论 #7542403 未加载
CJefferson大约 11 年前
I have also found people often unestimate realloc (but have never done the same level of investigation to find out just how clever it is!)<p>On several occasions I have wanted to use mmap, mremap, and friends more often to do fancy things like copy-on-write memory. However, I always find this whole area depressingly poorly documented, and hard to do (because if you mess up a copy-on-write call, it just turns into a copy it seems, with the same result but less performance).<p>While it&#x27;s good realloc is clever, I find it increasingly embarassing how badly C (and even worse, C++ which doesn&#x27;t even really have realloc (as most C++ types can&#x27;t be bitwise moved) memory allocation maps to what operating systems efficiently support.
评论 #7541359 未加载
asveikau大约 11 年前
This bothers me so much:<p><pre><code> buffer = realloc(buffer, capa); </code></pre> Yeah, &#x27;cause when it fails we didn&#x27;t need the old buffer anyway... Might as well leak it.
评论 #7541483 未加载
评论 #7543572 未加载
ctz大约 11 年前
The realloc implementation in this blog is incorrect: the passed in pointer must not be freed if realloc is called with a non-zero length and returns NULL. This will cause a double free in correct callers.<p>As someone else pointed out, the example call of realloc is also incorrect.<p>edit: also, malloc is incorrect for three reasons: 1) sbrk doesn&#x27;t return NULL on failure, 2) a large size_t length will cause a contraction in the heap segment rather than an allocation, and 3) sbrk doesn&#x27;t return a pointer aligned in any particular way, whereas malloc must return a pointer suitably aligned for all types.
评论 #7542350 未加载
kabdib大约 11 年前
I fixed a crippling bug on another platform that was taking down whole servers, because someone was depending on a clever realloc to behave well.<p>This is implementation coupling at its worst. Don&#x27;t do it.
评论 #7542419 未加载
picomancer大约 11 年前
This is really neat. Somehow I always assumed realloc() copied stuff instead of using the page table.<p>But say you have 4K page table size. You malloc() in turn a 2K object, a 256K object, and another 2K object, ending up with 2K, 256K, 2K in memory. Then your 256K is not aligned on a page boundary. If you realloc() the 256K it has to move since it&#x27;s surrounded by two objects. When you do that, you&#x27;ll wind up with the two pages on the end being mapped to multiple addresses. Which is actually just fine...Interesting...
评论 #7542956 未加载
crackerz大约 11 年前
And this is why OpenSource is awesome.
评论 #7544347 未加载
mjcohen大约 11 年前
In the original #define, the parameter is lower case &quot;c&quot; and the expansion uses upper case &quot;C&quot;.