TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Mysterious Moving Pointers

55 pointsby camblomquistabout 1 year ago

9 comments

userbinatorabout 1 year ago
IMHO this is another case where C++'s hidden layers of complexity hides bugs that would've been obvious in plain C. In fact for this particular use-case I'd probably use indices instead of pointers.
评论 #40034469 未加载
评论 #40034587 未加载
评论 #40035662 未加载
TillEabout 1 year ago
Storing a pointer to memory that you did not explicitly allocate is always a red flag, I think. You really need to understand how everything works, and be very careful.<p>I would default to just using std::unique_ptr&lt;Node&gt; in a situation like this, especially since using std::list suggests performance isn&#x27;t critical here, so the additional indirection probably doesn&#x27;t matter.
评论 #40034187 未加载
评论 #40035335 未加载
jnwatsonabout 1 year ago
This is a great reminder of the pox that was Microsoft of the early part of the millennium. Besides an allergy to investing in web standards, they were woefully behind in their language support. Their non-adoption of modern C++ standards held client security back for a decade, and arguable held language standards development back.
评论 #40034365 未加载
评论 #40034061 未加载
评论 #40035469 未加载
fransje26about 1 year ago
I must be misunderstanding something from this article. With:<p>struct Node {<p><pre><code> std::vector&lt;Connection&gt; connections; </code></pre> };<p>struct Connection {<p><pre><code> Node* from, to; </code></pre> };<p>Does this mean that to create the vector of connections, Nodes are created, and references are taken to store in the Connections? And then the Nodes are stored in the list, with std::move()?<p>I don&#x27;t understand why you would want to go down that road. Intuitively, I would assume that you are not safe from an object copy somewhere down the line and your graph then comes crashing down like a house of cards. Wouldn&#x27;t it make more sense to store the nodes as pointers? If you like to live dangerously, something like:<p>struct Graph {<p><pre><code> std::vector&lt;std::list&lt;Node\*&gt;&gt; nodes; </code></pre> };<p>Or better:<p>struct Graph {<p><pre><code> std::vector&lt;std::list&lt;std::unique_ptr&lt;Node&gt;&gt;&gt; nodes; </code></pre> };<p>The later will give you plenty of warnings if you do not copy Nodes around with std::move().<p>Or less performant, but maybe safer, std::shared_ptr&lt;Node&gt;, together with:<p>struct Connection {<p><pre><code> std::weak_ptr&lt;Node&gt; from, to; </code></pre> };<p>so that you have some check guarantees before access?
评论 #40040494 未加载
wakawaka28about 1 year ago
This is a noob mistake, not a huge mystery. It&#x27;s not always wrong to store raw pointers to STL container elements, but if you do then you must take care of reallocations.<p>If you find storing pointers to elements too perilous, you should probably just make a container of pointers instead.
评论 #40034621 未加载
评论 #40047886 未加载
gsliepenabout 1 year ago
There is a lesser known cousin to std::vector that doesn&#x27;t have to move nor copy its elements when adding new elements, and that is std::deque.
评论 #40037870 未加载
olliejabout 1 year ago
I know C++ the language but not the STL (the overwhelming abundance of UB and total lack of safety make it an anathema), so my question is why the STL allows&#x2F;requires non-move here copying here dependent on whether an object has a no throw move constructor?<p>Note I’m not asking about move constructor vs memmove&#x2F;cpy but rather the use of copy constructor vs move depending on exception behavior? Is it something like prefer no throw copy constructor over “throwing” move?
评论 #40034023 未加载
评论 #40033991 未加载
评论 #40034320 未加载
评论 #40034148 未加载
评论 #40033798 未加载
评论 #40034537 未加载
评论 #40035086 未加载
SleepyMyroslavabout 1 year ago
This is another nice reminder to people who build systems where there is only one language implementation. Implementations always change.
beyondCriticsabout 1 year ago
Don&#x27;t assume! The basic mantra of software development.