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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Creating a memory allocator for fun

7 点作者 averagedev超过 3 年前

2 条评论

JonChesterfield超过 3 年前
I think this post is missing that one cannot write a memory allocator that returns objects of different types in conforming&#x2F;correct C. I.e. you can&#x27;t write malloc because it violates the type aliasing restrictions.<p>This can be worked around by putting the allocator in a separate object file (no LTO) or shared library, or by annotating it with compiler attributes that locally switch off aliasing, or by hoping the compiler doesn&#x27;t exploit the UB inherent in the allocator to your ruin.<p>Same game in C++ as of the last time I checked, but at least there&#x27;s placement new and std::launder as workarounds, which strictly speaking need to be used when creating objects with malloc too.<p>&gt; no actual replacement occurring, rather a simple recasting, since both structs have the same memory footprint<p>Can&#x27;t do that either I&#x27;m afraid for much the same reason, need to memcpy over the memory and trust the compiler to elide said copy
评论 #30271700 未加载
averagedev超过 3 年前
Hi HN! Just a disclaimer that this is my own post. I thought you might find it interesting.