Chapter 3. Memory Management and Exception Safety
C++ has a great deal of compatibility with the C programming language. C++ retains pointers for representing and accessing specific memory addresses and provides manual memory management primitives via the new
and delete
operators. You can also seamlessly access from C++, the C Standard Library functions and C system calls or platform APIs of most major operating systems. Naturally, C++ code often deals with handles to various OS resources, like heap memory, open files, sockets, threads, and shared memory. Acquiring such resources and failing to release them could have undesirable consequences for your programs, showing up as insidious bugs, including memory leaks and deadlocks.
In this chapter, we look at ways of encapsulating pointers to dynamically-allocated objects using smart pointers to ensure that they are automatically deallocated when they are no longer needed. We then extend these techniques to non-memory resources. In...