Michael L Torrie <[EMAIL PROTECTED]> writes: > Manual memory management is often, in my opinion, a huge plus of C and > C++. With reference-counting smart pointers and destructors, memory > management in C++ is very straight-forward, fast, and safe. Just > understanding a bit about how C++ does scoping, and you can very easily > build and destroy entire data structures all without a single leak and > without having to rely on a garbage collector. Not saying a GC is bad; > simply that it's not always necessary.
Reference-counting smart pointers and destructors aren't really manual memory management, though. Reference-counting is a simple automatic garbage collection algorithm. It's got advantages, especially that its operation is deterministic and fairly simple, but also disadvantages such as the need to maintain reference counts, which can actually add signficant overhead. Of course, in C++, you'd simply not use them where that overhead would matter. You can also link in a conservative garbage collector, if you'd like. This flexibility is a double-edged sword, in my opinion. C++ is hugely complex and has a myriad of ways to do things, so it's really a tool that requires deep knowledge of the language, available libraries, and best practices to use well. > With only a few caveats, C++ programmers can easily move to programming > in Java and dealing with the GC dropping references right and left. But > it rarely works the other way. 9/10 core dumps in the BYU CS 240 class > are because of Java. It pollutes C++ programmers like nothing else! > One of the caveats for C++ programmers is the way that C++ guarantees > destruction when an object goes out of scope, especially when you rely > on the side effects. Guaranteed destruction doesn't translate directly > into Java, although the "finalize" keyword can help (if I recall > correctly). A good article on the idea is at > http://royontechnology.blogspot.com/2007/01/deterministic-destruction-of-objects-in.html Everyone (who wants to be a Real Programmer, at least) needs to learn how memory allocation works sooner or later. It's not Java's fault that it doesn't teach memory allocation. People who learn C++ as a first language make a lot of segfaults, too. :) If you haven't learned how memory allocation works yet, you're not yet a C++ programmer, so you're not 'polluted', you're just a neophyte. > It's always fun to catch java-isms in C or C++ code, the most prominent > one is the "type var = new type" expression, which is often used where a > static declaration would be far better (in C or C++). Of course using > smart pointers (reference-counting objects that hold a pointer), the > paradigm for use is much more java-like. Then you have to watch out for subtle interactions with STL containers and such, if I remember correctly. One of the disadvantages of having so many different ways of doing things is that not all the ways are compatible. --Levi /* PLUG: http://plug.org, #utah on irc.freenode.net Unsubscribe: http://plug.org/mailman/options/plug Don't fear the penguin. */
