Why C++ Talks About Resources Instead of Memory?
Yesterday I attended a talk by Bjarne Stroustrup on modern C++ at CERN (slides and recording are available). This was a historical event for me (had a couple of photos taken and shared with my non-technical partner which is very rare), I have started learning C++ maybe 15 years ago and have never had the opportunity to see Bjarne speak in person although I have been following his work for many years. The talk was excellent, and I learned a lot about the language's design philosophy and its evolution over the years.
Most technical talks usually leave you with a list of ideas to explore. This one left me with a single question. It wasn't because the talk was unclear, quite the opposite. One statement sounded so fundamental to C++'s design that I found myself replaying it afterwards, trying to reconcile it with years of hearing about one of C++'s most infamous problems.
Bjarne repeatedly emphasized that one of the central goals of C++ is preventing resource leaks. That sounded straightforward at first, but it immediately raised a question in my mind. If preventing resource leaks is such a fundamental design goal, why is C++ so well known for memory leaks? Aren't memory leaks one of the classic C++ bugs? if not the most classic? How can a language designed to prevent resource leaks be so notorious for memory leaks?
For a while, this felt contradictory. but after some reflection, I realized that the answer was not in the talk itself but in my own assumptions about what Bjarne meant by "resource."
The answer turned out to be that I was thinking too narrowly about the word resource.
Like many people, I instinctively equated resource with memory. After all, every object in a C++ program ultimately occupies memory. A socket object, a file stream, or a mutex all live somewhere in RAM, so it is tempting to think that memory is the only real resource and everything else is merely an implementation detail.
But that is not the abstraction C++ is built around. At least not…