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.

Why should I use a pointer rather than the object itself?

72 pointsby olouvabout 11 years ago

8 comments

stormbrewabout 11 years ago
&gt; note: in java, everything(except basic types) is a pointer. so you rather should ask the opposite: why do I need simple objects?<p>I think this is by far the most important piece of advice in that thread. It gets right to the heart of one of the most basic areas of confusion that programmers coming from garbage collected environments to ones with manual memory management have.<p>They need to understand that they have NOT been working in an environment <i>without pointers</i>, as they are often told to believe, but one in which <i>nearly everything is a pointer</i>. Understanding this is fundamental to understanding these differences and a 10,000 word essay about C++ allocation policies will not make it any clearer without dealing with that first.
评论 #7363643 未加载
评论 #7364008 未加载
评论 #7363700 未加载
GuiAabout 11 years ago
Related Stack Overflow question: &quot;C++: Why should `new` be used as little as possible?&quot;<p><a href="https://stackoverflow.com/questions/6500313/c-why-should-new-be-used-as-little-as-possible" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;6500313&#x2F;c-why-should-new...</a>
评论 #7363313 未加载
nsmnsfabout 11 years ago
Can someone explain RAII to me? In the way that it&#x27;s commonly used, it seems like it really means &quot;stack variables that go out of scope are deallocated&quot;, which seems kind of, well, duh? I don&#x27;t really understand why it&#x27;s treated like a big deal.<p>(I mostly write C + Obj-C)
评论 #7363203 未加载
评论 #7362918 未加载
评论 #7363093 未加载
评论 #7362992 未加载
评论 #7362953 未加载
评论 #7363564 未加载
评论 #7363373 未加载
评论 #7363035 未加载
评论 #7363297 未加载
Sunchoabout 11 years ago
I find it interesting that nobody mentions iterators anywhere in this discussion. One of the main uses of pointers is to iterate through data. Pointers are the most basic form of iterators in C++. STL iterators are based on, and often implemented in terms of, pointers.
joe_the_userabout 11 years ago
One interesting thing is that the QT framework prefers the use of pointers over reference variables. There&#x27;s actually some reasonable logic here. Pointer syntax clearly shows you are acting on something in a different way than you act on an &quot;ordinary&quot; object. Reference object syntax is the same as ordinary object syntax but you are making changes that will go beyond the local scope and that is thus less obvious.<p>With this approach, you can still allocate objects on the stack - you then take their address and pass that to a subroutine.
评论 #7363175 未加载
orionblastarabout 11 years ago
I audited a few MOOC classes.<p>They don&#x27;t even bother to teach pointers, the stack, or heap. It just confuses the students they claim. Also teach students to store the value of PI into an Integer. Use DRJava and Java 6 and some bloated JAR library that makes Windows crash.<p>When I studied computer science in 1986, the very first things we learned where how memory and binary worked, plus what a pointer was and why it should be used and how to manage memory so it would not crash and lock up. We learned how to debug as well.<p>Most comp sci graduates that I talk to know almost nothing of how a pointer works, or the stack or heap, and binary might as well be Tagalog to them.<p>I think that over the years as the demand for IT workers increased, they relaxed the standards they used in teaching computer science and programming.<p>Any good MOOC should teach:<p>Pointers<p>The Stack<p>The Heap<p>Binary<p>Hexadecimal<p>How to debug code<p>Assembly Language instructions the code gets converted into and how to debug that
EGregabout 11 years ago
One is allocating on the stack and one on the heap.<p>Allocating on the heap means you have to delete it yourself in C++.<p>It&#x27;s pretty staightforward, no?
Jonathan_Swiftabout 11 years ago
It&#x27;s not always obvious. Quite commonly one is used when the other would be better, even by otherwise good coders.<p>To beat the subject completely to death:<p>Pointers, References and Values<p>Passing Parameters, Returning Results, and Storing Member Variables with Musings on Good C++ Style<p><a href="http://www.warplife.com/tips/code/c++/memory-management/parameters/" rel="nofollow">http:&#x2F;&#x2F;www.warplife.com&#x2F;tips&#x2F;code&#x2F;c++&#x2F;memory-management&#x2F;para...</a>