dynamic memory review l what is static, automatic, dynamic variables? why are dynamic(ally...

11
Dynamic Memory Review what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed what is program stack? Function (invocation) frame? what is a heap? how is it used? what is new and delete operations? How are they used? how is dynamically allocated variable accessed? assigned value at declaration time? what is memory leak? loose pointer (again)? how are dynamic arrays allocated? deallocated? how can a pointer be passed by value/by reference to a function? what are dynamically allocated objects? 1

Upload: frederick-gaines

Post on 18-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

Objects Containing Dynamic Members l if we want to create an object that can shrink and grow as needed? class MyClass{ public: MyClass(int); // constructor private: int *d; int size; }; l with the following constructor: MyClass::MyClass(int n){ size=n; d = new int[size]; } l then the following declaration MyClass myobj(5); creates an object containing an array of 10 integer variables MyClass myobj2(10); // 10-element array 3

TRANSCRIPT

Page 1: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Dynamic Memory Review

what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed

what is program stack? Function (invocation) frame? what is a heap? how is it used? what is new and delete operations? How are they used? how is dynamically allocated variable accessed? assigned value at

declaration time? what is memory leak? loose pointer (again)? how are dynamic arrays allocated? deallocated? how can a pointer be passed by value/by reference to a function? what are dynamically allocated objects?

1

Page 2: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Objects Containing Dynamically Allocated Members

Page 3: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Objects Containing Dynamic Members if we want to create an object that can shrink and grow as needed?

class MyClass{public: MyClass(int); // constructorprivate: int *d; int size;};

with the following constructor:MyClass::MyClass(int n){ size=n; d = new int[size];}

then the following declaration MyClass myobj(5);

creates an object containing an array of 10 integer variablesMyClass myobj2(10); // 10-element array

3

Page 4: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked destructor is a function that is called (automatically) when object goes out of scope

class MyClass{public: MyClass(int); // constructor ~MyClass(); // destructorprivate: int *d; int size;};

name of destructor tilde (~) and name of class MyClass::~MyClass(){ delete [] d;}

no need to deallocate automatic variables destructor is never called explicitly and does not accept parameters note that destructor is called on every local object when function finishes

4

Page 5: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Copy Constructor what if we have this function (note that the object is passed by value):

void myfunc (MyClass); if we invoke it as follows myfunc(myobj); what happens? the function is passed a reference to the same array - copy of the array is not

created copy constructor is invoked automatically when a new copy of an object is

implicitly created when function is passed an object by value when function returns an object

copy constructor can be used explicitly copy constructor is not called when one object is assigned to another (more on

that later)

5

Page 6: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Copy Constructor (cont.) copy constructor is a constructor that accepts an object of the same class passed by

referenceclass MyClass{public: MyClass(int); // regular constructor MyClass(const MyClass&); // copy constructorprivate: int *d; int size;};

defined as followsMyClass::MyClass(const MyClass& org){ size=org.size; d = new int[size]; for(int i=0; i< size; ++i) d[i]=org.d[i];}

note that copy constructor can access private members of the parameter object can be invoked explicitly: MyClass newobj(myobj);

6

Page 7: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Assignment Overloading what do you think this operation does? secondObj=firstObj; copies the value of pointer leaking the array of the old object and not creating a copy of the array in the

new object assignment needs to be overloaded assignment overloading operator accepts by reference the object on the right-hand-side of equation

and is invoked by the object on the left-hand-side: lhsobj = rhsobj;

class MyClass{public: … void operator= (const MyClass&);private: int *d; int size;};

can define as follows (not quite right yet):void MyClass::operator= (const MyClass& rhs){

size=rhs.size;delete [] d;d=new int[size];for (int i=0; i < size; ++i) d[i]=rhs.d[i];

}

7

Page 8: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Self-Assignment Protection what happens if you do this assignment? myobj=myobj;

It is legal in C++. Is our overloaded assignment going to handle it right? this is a reserved keyword. It is a pointer to the object that invokes the member

function. It is passed implicitly to every member function

assignment overloading (still not quite right):void MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; }}

8

Page 9: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Stackable Assignment what happens if you do this assignment?

thirdObj = secondObj = firstObj;

here is the definition that gets above code to work correctlyMyClass& MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; } return *this; // return lhs}

note the return value of the function, it is a reference to an object returned object refers to object used in return-statement similar to pass-by-reference parameter

9

Page 10: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

The BIG Three big three - copy constructor, overloaded assignment and

destructor expert opinion - if you need any one of them - most probably you

will need all three they are not syntactically related but it is usually safer to define all

three if you think you’d need at least one

10

Page 11: Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function

Objects with Dynamic Members Review

What are dynamically allocated objects Objects containing dynamically allocated members? What may be potential problems with the latter?

What are the big three operations? What is a destructor? Why is it needed? When is it executed? How is it

declared/defined? What is a copy-constructor? Why is it needed? When is it executed? How is it

declared/defined? What is operation overloading? What is overloaded assignment? Why is it

needed for objects with dynamic members? How is it declared/defined? What is this-pointer? What is protection against self-assignment? How is

this-pointer used for that? What is stackability of an operator? How can overloaded assignment be

made stackable?

11