Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function."— Presentation transcript:

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

2 Objects Containing Dynamically Allocated Members

3 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

4 Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked l destructor is a function that is called (automatically) when object goes out of scope class MyClass{ public: MyClass(int); // constructor ~MyClass(); // destructor private: int *d; int size; }; name of destructor tilde ( ~ ) and name of class MyClass::~MyClass(){ delete [] d; } l no need to deallocate automatic variables l destructor is never called explicitly and does not accept parameters l note that destructor is called on every local object when function finishes 4

5 Copy Constructor l 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? l the function is passed a reference to the same array - copy of the array is not created l copy constructor is invoked automatically when a new copy of an object is implicitly created n when function is passed an object by value n when function returns an object l copy constructor can be used explicitly l copy constructor is not called when one object is assigned to another (more on that later) 5

6 Copy Constructor (cont.) l copy constructor is a constructor that accepts an object of the same class passed by reference class MyClass{ public: MyClass(int); // regular constructor MyClass(const MyClass&); // copy constructor private: int *d; int size; }; l defined as follows MyClass::MyClass(const MyClass& org){ size=org.size; d = new int[size]; for(int i=0; i< size; ++i) d[i]=org.d[i]; } l note that copy constructor can access private members of the parameter object can be invoked explicitly: MyClass newobj(myobj); 6

7 Assignment Overloading what do you think this operation does? secondObj=firstObj; l copies the value of pointer leaking the array of the old object and not creating a copy of the array in the new object l 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; }; l 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

8 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 l 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

9 Stackable Assignment l what happens if you do this assignment? thirdObj = secondObj = firstObj; here is the definition that gets above code to work correctly MyClass& 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 } l note the return value of the function, it is a reference to an object n returned object refers to object used in return-statement n similar to pass-by-reference parameter 9

10 The BIG Three l big three - copy constructor, overloaded assignment and destructor l expert opinion - if you need any one of them - most probably you will need all three l they are not syntactically related but it is usually safer to define all three if you think you’d need at least one 10

11 Objects with Dynamic Members Review l What are dynamically allocated objects Objects containing dynamically allocated members? What may be potential problems with the latter? l What are the big three operations? l What is a destructor? Why is it needed? When is it executed? How is it declared/defined? l What is a copy-constructor? Why is it needed? When is it executed? How is it declared/defined? l 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? l What is stackability of an operator? How can overloaded assignment be made stackable? 11


Download ppt "Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function."

Similar presentations


Ads by Google