C++_memory_management_idioms

C++_memory_management_idioms - C+ Memory Management Idioms...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language We’ll look at 4 important ones in C++ Copy constructor trick for assignment Ensures release of existing resource and acquisition of the new resource both succeed or fail together RAII (a.k.a. Guard) ties dynamic resources to other (esp. automatic) scopes Reference counting ties dynamic lifetime to a group of references Copy-on-write allows more efficient management of multiple aliasing
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ memory management idioms Copy Constructor Trick for Assignment Cleanup/assignment succeed or fail together class Array { public: Array(unsigned int) ; Array(const Array &); // assume copy constructor makes a deep copy ~Array(); Array & operator=(const Array &a); private: int * ints_; size_t size_; }; if (&a != this) { Array temp(a); std::swap(temp.ints_, ints_); std::swap(temp.size_, size_); } return *this; }
Background image of page 2
CSE 332: C++ memory management idioms Resource Acquisition Is Initialization (RAII) Also referred to as the “Guard Idiom” However, the term “RAII” is more widely used in C++
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

C++_memory_management_idioms - C+ Memory Management Idioms...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online