Hasty Briefsbeta

Bilingual

Modern C++ for C Programmers: Part 5 - Bert Hubert's writings

a day ago
  • C++ optimizes memory by allowing copy elision and return value optimization, unlike C.
  • Smart pointers like `std::shared_ptr` and `std::unique_ptr` manage memory automatically, with `std::unique_ptr` having zero overhead.
  • `std::move` transfers ownership without copying, enabling efficient resource management via move constructors.
  • Smart pointers support polymorphism with base and derived classes when destructors are virtual.
  • Placement new allows constructing objects in pre-allocated memory for advanced use cases like shared memory.
  • Modern C++ code should minimize explicit `new`/`delete`, using `std::unique_ptr` and `std::shared_ptr` judiciously.