REVIEW - C++ Memory Management - Write leaner and safer C++ code using proven memory-management techniques


Title:

C++ Memory Management

Write leaner and safer C++ code using proven memory-management techniques

Author:

Patrice Roy

Publisher:

Packt Publishing (2025)

Pages:

434

Reviewer:

Fabian Schutze

Reviewed:

July 2025

Rating:

★★★★★


Highly recommended.

C++ Memory Management is a new book by Patrice Roy. It is published by Packt Publishing, and I usually approach its books with trepidation. However, the topic and Patrice’s cachet lured me to read it, and I was greatly rewarded. Patrice’s lucid writing and expert guidance shine through the pages. The book builds at first a foundation for subsequent chapters. Then, implicit and explicit memory management techniques are discussed. Based on these techniques, the book presents implementations of efficient containers. Finally, the book concludes with a discussion of potential C++ features currently examined by the ISO committee.

The first part of the book lays the foundation. It discusses all the necessary aspects of memory allocations in C++. That involves objects, references, pointers, type casts, and other perilous aspects of memory management. The presentation is concise and broad.

The second part discusses implicit memory management techniques. This includes the RAII idiom, how to use smart pointers, and designing smart pointers. The usage of smart pointers aligns with the C++ core guidelines but describes the motivation behind them. For example, a unique or shared pointer should not be chosen based on its number of users, but on whether its time and place of destruction are known in advance.

The following section discusses explicit memory management techniques. I found this section particularly rewarding. First, all forms of operator new are presented. After reading this chapter, I could write a cache-aligned std::vector, as I had wanted for some time. The next four chapters apply the presented material in various scenarios. The following chapter implements a memory leak detector. I would have appreciated a comparison with industry-strength memory sanitizers for additional context. Another chapter discusses memory arenas. The other chapter discusses topics of prime interest for embedded devices (no-throw allocations, using placement new as an abstraction over memory-mapped devices). Finally, the last chapter discusses deferred reclamation.

Armed with this knowledge of explicit and implicit memory management, Patrice progresses to writing efficient containers (a vector and forward list). The first chapter was particularly eye-opening for me: Patrice presents how the operator new combines memory allocation and initialization of objects, which can lead to a subpar vector implementation. Patrice also uses type traits to deduce the optimal choice of copy and move operations. A wonderful side-effect of reading this chapter is learning about many aspects of C++’s object model. My slight disappointment is that the author motivates a sophisticated vector implementation by stating that a naive implementation is slow without backing it up with measurements. The subsequent chapter presents a different implementation with implicit memory management.

Patrice leverages his participation in the ISO standardization process and concludes with a discussion of possible new C++ features. In particular, he discusses a typed allocator new, trivial relocation, and starting object lifetime without constructors.

All in all, this is a terrific book. I encourage everyone to buy a copy of it and work through it. I’m sure it’ll be rewarding for anyone.

Website: https://www.packtpub.com/en-gb/product/c-memory-management-9781805129806

Code site: https://github.com/PacktPublishing/C-Plus-Plus-Memory-Management






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.