Summary
In this chapter, we explored various causes of bad code in C++ and how a lack of knowledge in modern C++ practices can lead to inefficient, error-prone, or undefined behavior. By examining specific examples, we highlighted the importance of continuous learning and adaptation to keep up with the evolving features of C++.
We began by discussing the pitfalls of using raw pointers and manual memory management, showing how modern C++ practices such as std::vector
can eliminate the need for manual memory management and reduce the risk of memory leaks. The advantages of using std::unique_ptr
for exclusive ownership and std::shared_ptr
for shared ownership were emphasized, while common issues such as inefficient memory allocation, unnecessary copying, and cyclic dependencies were highlighted.
In the context of std::shared_ptr
, we demonstrated the benefits of using std::make_shared
over the constructor to reduce memory allocations and improve performance. The efficiency gained...