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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Understanding Memory Management, Part 2: C++ and RAII

40 点作者 ibobev2 个月前

5 条评论

williamcotton2 个月前
&gt; <i>Everything we&#x27;ve seen here is still normal C, but often we want to associate a function with a type. For instance, the area function we have shown above only works with rectangles, but what if we had circles as well? We&#x27;d end up with two functions, one called area_rectangle and one called area_circle.</i><p>This is not to call the article into question but only to show that C11 does support generics, allowing for:<p><pre><code> printf(&quot;Rectangle area: %.2f\n&quot;, area(rect)); printf(&quot;Circle area: %.2f\n&quot;, area(circle)); </code></pre> Full code here:<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;williamcotton&#x2F;a8f429e891cbba5abfadcc0a929dda00" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;williamcotton&#x2F;a8f429e891cbba5abfadcc...</a>
评论 #43310792 未加载
评论 #43308802 未加载
einpoklum2 个月前
I really wish we would adopt the term CADRe intead of RAII:<p>&quot;Constructor Allocates, Destructor Releases&quot;<p>is much clearer a phrase than<p>&quot;Resource Acquisition Is Initialization&quot;<p>which, in English, can mean any number of things, particularly initialziation that&#x27;s distinct from construction.
评论 #43308649 未加载
评论 #43308624 未加载
flohofwoe2 个月前
&gt; ...many C programs will compile just fine with a C++ compiler<p>Unfortunately(?) that&#x27;s only true for C code written in the early 90s or specifically for the common C&#x2F;C++ subset (which is essentially a subset of both C and C++ based on a non-standard snapshot of C ca 1995).<p>Since C99, the two languages have diverged enough that C is no longer a subset of C++, and IMHO propagating the idea that there&#x27;s a &#x27;C subset&#x27; in C++ 25 years after the &#x27;schism&#x27; is harmful both for C and C++.
评论 #43308113 未加载
评论 #43308474 未加载
评论 #43308107 未加载
einpoklum2 个月前
Ironically, this post suffers from excessive C&#x27;ism and older-C++&#x27;ism.<p>Specifically, it encourages the use of pointers for passing-by-reference, and when working with class hierarchies. This is inappropriate and overly dangerous; and references (especially const references) should be prefered, or passing-by-value (and utlilization of copy elisions).<p>The post also suggests people perform raw allocations with `new`. That is also discouraged; see C++ Core Guideline R11. [1]<p>[1]: <a href="https:&#x2F;&#x2F;isocpp.github.io&#x2F;CppCoreGuidelines&#x2F;CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly" rel="nofollow">https:&#x2F;&#x2F;isocpp.github.io&#x2F;CppCoreGuidelines&#x2F;CppCoreGuidelines...</a>
评论 #43308634 未加载
评论 #43323084 未加载
otherayden2 个月前
As a CS undergrad who has worked with C and C++ for assignments but generally sucks at it, I needed this article. Thanks so much for sharing!