> 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.
Related Stack Overflow question: "C++: Why should `new` be used as little as possible?"<p><a href="https://stackoverflow.com/questions/6500313/c-why-should-new-be-used-as-little-as-possible" rel="nofollow">https://stackoverflow.com/questions/6500313/c-why-should-new...</a>
Can someone explain RAII to me? In the way that it's commonly used, it seems like it really means "stack variables that go out of scope are deallocated", which seems kind of, well, duh? I don't really understand why it's treated like a big deal.<p>(I mostly write C + Obj-C)
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.
One interesting thing is that the QT framework prefers the use of pointers over reference variables. There's actually some reasonable logic here. Pointer syntax clearly shows you are acting on something in a different way than you act on an "ordinary" 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.
I audited a few MOOC classes.<p>They don'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
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's pretty staightforward, no?
It'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://www.warplife.com/tips/code/c++/memory-management/para...</a>