How do you recommend people handle memory management in C++?
My recommendation is to see memory as just one resource among many (e.g. thread handles, locks, file handles, end sockets) and to represent every resource as an object of some class. For example, memory may be used to hold elements of a container or characters of a string, so we should use types such as vector
Wherever possible, I recommend the use of such "resource handles" simply as scoped variables. In that case, there is no explicit memory management that a programmer can get wrong. When an object's lifetime cannot easily be scoped, I recommend some other simple scheme, such as use of "smart" pointers (appropriate ones provided in C++0x) or representing ownership as membership in some collection (that technique can be used in embedded systems with Draconian time and space requirements). These techniques have the virtues of applying uniformly to all kinds of resources and integrating nicely with a range of error-handling approaches.
Only where such approaches become unmanageable - such as for a system without a definite resource management or error handling architecture or for a system littered with explicit allocation operations - would I apply GC. Unfortunately, such systems are very common, so I consider this is a very strong case for GC even though GC doesn't integrate cleanly with general resource management (don't even think of finalizes). Also, if a collector can be instrumented to report what garbage it finds, it becomes an excellent leak detector.
When you use scoped resource management and containers, comparatively little garbage is generated and GC becomes very fast. Such concerns are behind my claim that "C++ is my favorite garbage collected language because it generates so little garbage."
I had hoped that a garbage collector which could be optionally enabled would be part of C++0x, but there were enough technical problems that I have to make do with just a detailed specification of how such a collector integrates with the rest of the language, if provided. As is the case with essentially all C++0x features, an experimental implementation exists.
There are many aspects of garbage collection beyond what I mention here, but after all, this is an interview, not a textbook.
On a less serious note, do you think that facial hair is related to the success of programming languages?
I guess that if we look at it philosophically everything is related somehow, but in this case we have just humor and the fashion of the times. An earlier generation of designers of successful languages was beardless: Backus (Fortran), Hopper (COBOL), and McCarthy (Lisp), as were Dahl and Nygaard (Simula and Object-Oriented Programming). In my case, I'm just pragmatic: While I was living in colder climates (Denmark, England, and New Jersey), I wore a beard; now I live in a very hot place, Texas, and choose not to suffer under a beard. Interestingly, the photo they use to illustrate an intermediate stage of my beard does no such thing. It shows me visiting Norway and reverting to cold-weather type for a few days. Maybe there are other interesting correlations? Maybe there is one between designer height and language success? Maybe there is a collation between language success and appreciation of Monty Python? Someone could have fun doing a bit of research on this.
References
- The A-Z of Programming Languages: AWK
- The A-Z of Programming Languages: Ada
- The A-Z of Programming Languages: ASP
- The A-Z of Programming Languages: BASH/Bourne-Again Shell
- The A-Z of Programming Languages: Forth
- home pages
- book
- any of my papers on C++0x
- publications page
- Doug Gregor's home pages
- technical FAQ
- JSF++ coding rules
- short list
Latest on C/C++
- A future without programming
- Microsoft, Novell eye Moonlight beta, system management
- Coverity assists software architects
- World without Linux
- Multicore: New chips mean new challenges for developers
- Microsoft advances embedded apps
- Symbian releases free application development tool
- Six Scripting Languages Your Developers Wish You'd Let Them Use
- Microsoft woos developers under the Silverlight
- The A-Z of Programming Languages: C#
Development Essentials
- Top 10 wicked cool algorithms
- Facebook app verification fee draws criticism
- Microsoft's openness stressed
- Yahoo's developer platform to launch this week
- Microsoft starts new developer portal
- Google API allows creating apps that can track laptops
- PHP, JavaScript, Ruby, Perl, Python, and Tcl Today: The State of the Scripting Universe
- Analyst: In-house app development fraught with waste
- Is unit testing doomed?
- Open source still the best way to develop software
TechWorld Jobs (beta)
Recent Jobs
Whitepapers
- Still Sneaking In: The Threats Your Security Tools Aren't Telling You About
- Discover the advantages of an open architecture multi-vendor network solution
- Realizing the Value of Unified Communications
- Mimosa™ NearPoint™ for Microsoft® Exchange Server: Email Archiving 101
- Choices in Storage Architecture for Oracle Environments
TechWorld Blogs
-

TalkingTech
The view from the top of IT with TechWorld Editor Rodney Gedda
-

Entrenched
Cooking up better code, IDG's developers reveal some of their secrets
-

Broadband Voice
Darren Pauli digs in from the front line of Australia's broadband battleground
Recent blog posts
- A Novell approach to business
- An open storage stack? I like the sound of that
- The mobile clone wars: fighting for a better phone experience
- Stopping the "Clean Feed"
- Identifying web platforms
- Clean Feed ‘not technically possible’
- No Clean Feed - well duh!
- Conroy's content cops still on the cards
- Will open source ruin the economy? Please help
- Linux kernel 2.6.27 is out!
Recent comments
- A real alternative?
20 hours 52 min ago - Hello this is Brianna
2 days 6 hours ago - Turn any PC into a media center
2 days 20 hours ago - How About the Correct Title?
3 days 11 hours ago - who are you kidding?
3 days 16 hours ago - Seriously, how much did they pay for this advertisement
5 days 7 hours ago - SF Bay Area - free Seminar on Enterprise Cloud Computing
5 days 10 hours ago - video conferening but not telepresence...
5 days 17 hours ago - SAMSUNG OLED 40" TECHNOLOGY
6 days 2 hours ago - What was the question again, oh well this was prepared earlier
1 week 1 day ago - Worldwide broadband prices continue to drop which means ? in AU
1 week 1 day ago - Not a Problem Here in Australia and New Zealand
1 week 3 days ago - Clear the air
1 week 4 days ago - Tabbed browsing, Quick Find,
2 weeks 2 hours ago - Microsoft details plans for new social bookmarking tool
2 weeks 1 day ago - There is a 3rd party tool
2 weeks 3 days ago - Demise of Windows
2 weeks 3 days ago - new OS
2 weeks 3 days ago - Re: Favicon
2 weeks 4 days ago - Multi Camera Kino
2 weeks 4 days ago




Comments
C++ A computer language for ever
C++ - A computer language that will will remain with the computer technology evolution.