Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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?

2 Objects Containing Dynamically Allocated Members

3 Objects Containing Dynamic Members
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; }; with the following constructor: MyClass::MyClass(int n){ size=n; d = new int[size]; } then the following declaration creats an object containing an array of 5 integers MyClass myobj(5); and this contains an array of 10 integers (note both objects belong to same class) MyClass myobj2(10); // 10-element array

4 Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked why? destructor is a function that is called implicitly (without mentioning its name) 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; } no need to deallocate automatic variables destructor is never invoked explicitly and does not accept parameters note that destructor is called on every local object when function ends

5 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 implicitly when a new copy of an object is created: when function is passed an object by value when function returns an object copy constructor can be invoked at object declaration, how? copy constructor is not invoked when one object is assigned to another (more on that later)

6 Copy Constructor (cont.)
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; }; 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]; } note that copy constructor can access private members of the parameter object can be invoked at object declaration: MyClass newobj(myobj);

7 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]; }

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 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]; }

9 Stackable Assignment 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 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

10 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

11 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?


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

Similar presentations


Ads by Google