Title: CSE 212 Prof' Izaguirre
1CSE 212Prof. Izaguirre
- Lecture 13 Classes
- Part 3 Memory Management
2Outline
- Objective study memory management at the class
level continuing a matrix example. Understand
memory leaks and dangling references in the
context of classes, and how to fix them using
cloning behavior for the copy constructor and the
assignment operator - Review
- Parts of a class (continued)
- Constructors
- (cloning) copy constructor
- (cloning) assignment operator
- Destructors
- Resource allocation is initialization (RAII) (new
and delete) - Memory leaks
- Dangling references or pointers
- Object initialization (first 2 cases)
3Review I
- What are the default behaviors of the BIG
THREE default constructor, copy constructor,
and assignment operator?
4Understanding vectors I
5Understanding vectors III
6Operator Overloading
where a is an object of class A, b is an object
of class B, and c is an object of class C.
7Rules for object creation and destruction I
- Constructors initialize an object in addition,
they may obtain resources (such as memory for
variably-sized objects, e.g., a Matrix) - Similarly, destructors are called by the runtime
to release resources that have been allocated by
an object (such as memory!)
8Rules for object creation and destruction II
- Consider Matrix class
- MatrixMatrix(int rows, int cols, NumericT
value) - assert((rowsgt0)(colsgt0))
- try
- a new NumericTrowscols
-
- catch (...)
- throw / rethrow the exception - let caller
handle it / -
- for (int i0 iltrowscols i)
- aivalue
- my_rowsrows
- my_colscols
-
-
- Matrix()
- if (a!0)
- delete a
-
9Object creation destruction I
- Local objects constructed each time thread of
control passes through declaration of local
object. - They are destroyed each time the local objects
block is exited. - Destructors are executed in reverse order of
their construction - void f()
- Matrix A(10,10)Matrix B(10,10)
- // process A,B
- // destroy B, then A, before exiting f()
- What would happen if we did not define the
destructor for Matrix?
10Object creation destruction II
- Copying objects remember the default
member-wise copy of the copy constructor and the
assignment operator does not work well when we
have reference or pointer data members. Enumerate
what goes wrong in this block - void f()
- Matrix A(10,10)
- Matrix B(A)
- Matrix C(1,1)
- C B cout ltlt Display matrix ltlt B ltlt
endl
11Object creation destruction III
- These errors can be fixed by defining a cloning
behavior for the copy constructor and assignment
operator. Consider this first implementation - MatrixMatrix(const Matrix rhs) //member
initialization my_rows(rhs.rows()), my_cols
(rhs.cols()) - a new NumericT ( my_rows my_cols )
- // copy each element of rhs into
- // my_matrix_ptr
-
12Object creation destruction IV
- Now, consider the assignment operator
- Matrix Matrixoperator(const Matrix rhs)
- assert(this ! rhs) // self assignment
invalid ( WHY?) - my_rows rhs.rows
- my_cols rhs.cols
- a new double ( my_rows my_cols )
- // copy each element of rhs.a into a
-
- return // return what???
-
- What would happen in the execution of command f()
now?
13Object creation destruction V
- Finally, fix the assignment operator
- Matrix Matrixoperator(const Matrix rhs)
- assert(this ! rhs) // self assignment
invalid ( WHY?) - my_rows rhs.rows
- my_cols rhs.cols
-
- a new NumericT ( my_rows my_cols )
- // copy each element of rhs.a into a
- // my_matrix_ptr
- return // return what???
-
- Things seem safe now
14Object creation destruction VI
- Summarizing, what we have seen
- A memory leak is
- A dangling reference or pointer is
- When is the default copy constructor and
assignment operator behavior sufficient? - What are the steps to clone an object using the
copy constructor? - What else has to be done when defining cloning
for the assignment operator?
15Object creation destruction VII
- Global, namespace or class static objects, are
created once at the start of the program and
destroyed at termination - Local objects are created each time their
declaration is encountered and destroyed upon
exit from the block - Static local objects are created once the first
time their declaration is encountered and
destroyed at termination
16Object creation destruction VIII
- A free-store object is created using new and
destroyed using delete - A nonstatic member object is created when the
parent is created and destroyed when the
parent is destroyed - An array element, which is created and destroyed
when the array of which it is an element is
created and destroyed - A temporary object, which is created as part of
the evaluation of an expression and destroyed at
the end of the full expression