Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.

Similar presentations


Presentation on theme: " Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools."— Presentation transcript:

1

2  Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools

3  The new and delete operators  C++ does not have garbage collection  but it does have deterministic destructors!  Can deallocate any resource automatically  not just memory!  e.g., can unlock a mutex or close a connection  when a local object goes out of scope  when you use delete  no need for finally

4  Two versions:  Scalar (single object):  T* p = new T;// Calls constructor  delete p;// Calls destructor  Array:  T* p = new T[n];// p points to first element  delete [ ] p;  delete style must match the allocation ([ ])  Failing to delete is a memory leak

5  “Resource Acquisition is Initialization”  Memory is just one of many resources  Treat all resources equally in C++:  Have a constructor acquire them  Have the destructor release them  Example: file streams  Example: raii.cpp

6  Objects that emulate pointers  They hold the real pointer  but the wrapper object lives on the stack  its destructor calls delete on the real pointer  Overloaded operators:  *  ->

7  It runs twice!  First: it must return a “pointer-like thing”  Next: operator-> is called again on that return value  Eventually a raw pointer must be returned struct Foo {int x; int y;}; class FooWrapper { Foo* pf; public: FooWrapper(Foo* p) : pf(p) {} Foo* operator->() { cout << "returning a Foo*\n"; return pf; } }; int main() { Foo f = {1,2}; FooWrapper fw(&f); cout x << '\n'; cout y << '\n'; } /* Output: returning a Foo* 1 returning a Foo* 2 */

8  Creating a simple smart pointer  SafePtr.cpp (generic version)  smart.cpp (multi-level)  unique_ptr (uniqptr1-3.cpp, deleter1.cpp)  unique_ptrs are not copyable  shared_ptr (sharedptr.cpp, deleter2.cpp)  shared_ptrs increment their reference count when copied  And delete the raw pointer when count == 0

9   not implemented in gcc :-( 

10  Does the following before returning a pointer:  Allocates needed memory on the heap ▪ calls the library function operator new( )  Initializes the object by calling the proper constructor

11  Does the following before returning a pointer to the first element:  Allocates needed memory on the heap ▪ calls the library function operator new[ ]( )  Initializes each object by calling the proper constructor

12  Does 2 important things:  Calls the destructor for the object  Returns the memory to the free store ▪ via the library function void operator delete(void*)

13  Calls the destructor for each object in the array  Returns the memory to the free store  via void operator delete(void*);  You must use delete [ ] for arrays

14  You can overload all 4 memory functions:  operator new(size_t)  operator new[ ](size_t)  operator delete(void* )  operator delete[ ]( void*)  Can be useful for tracing memory operations  The array versions are seldom used  See memory.cpp

15  You can manage heap on a class basis  Just provide operator new( ) and operator delete( )  As member functions  Must be static ▪ Because they’re stand-alone functions, of course ▪ Even if you don’t declare them so, they will still be static  Example: memory2.cpp

16  If you want to disallow allocating object on the heap, declare non-public class allocation functions: protected: void* operator new(size_t){return 0;} void operator delete(void*) {}  (It is an interesting mystery that on some platforms, bodies are required for these functions when you declare them)

17  A special-purpose version of the new operator used by library developers  see Lab 3  (you will be tested on it)

18


Download ppt " Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools."

Similar presentations


Ads by Google