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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

C++ Constructors, Memory, and Lifetimes

47 点作者 blackhole大约 4 年前

3 条评论

dxuh大约 4 年前
As you are officially discouraged to use new and delete directly (<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>) and I fully agree with this guideline, I was disappointed to see new being mentioned so early and not immediately being followed up with std::unique_ptr and std::shared_ptr. Especially because you introduced the constructor&#x2F;destructor subsection with how destructors can be used to free memory. A general mention of std::shared_ptr would have been expected as well, I think.<p>It is unfortunate that many learning resources don&#x27;t show modern C++ so the language is perceived worse than it is and also more bugs are written, than is necessary.
评论 #27017117 未加载
评论 #27017225 未加载
zabzonk大约 4 年前
This is full of misunderstandings. When variables are allocated on the stack, the space for them with all compilers I have ever used (or written) is allocated on the stack once by adjusting the stack pointer - i.e. for all of the variables of the function. There is no pushing or popping - function variables have addresses on the stack. When the function ends, the stack pointer is reset to it&#x27;s original value, possibly with destructors for the things on the stack being called.<p>Also, in modern compilers, parameter variables are mostly passed to functions via registers, with the stack not being involved at all.
评论 #27017387 未加载
评论 #27017718 未加载
评论 #27016999 未加载
评论 #27017997 未加载
layoutIfNeeded大约 4 年前
<i>&gt;This means it allocates memory from the heap, named after the heap data structure</i><p>Oh my god, please don&#x27;t write articles about things that you barely understand!
评论 #27017920 未加载