Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.

Similar presentations


Presentation on theme: "Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference."— Presentation transcript:

1 Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference operators? l How can a pointer be assigned a value? l How can a value of a memory location referred to by a pointer be accessed? l What is a constant pointer? What is a pointer to a constant? l What is the relationship between array name and a pointer? l What is the operation to move pointer to the next/previous memory location? l What is null pointer? What is lose pointer problem? l Can pointers be used with objects? How can a method be invoked on an object using pointer? what is -> operator? 1

2 Dynamic Memory Allocation

3 Why Dynamic Memory l when a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated l not all memory can be allocated or efficiently allocated at the compilation time n we are manipulating an set of data of arbitrary size –what if we allocate an array too small? –what if we allocate an array too large? l dynamic memory allocation – memory allocation at execution time under the direct control of programmer l dynamic memory allocation is more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow l heap - the system structure where the memory is allocated l heap is separate for every program and it is removed when the program finishes 3

4 new and delete new and delete - operations used to dynamically manipulate memory new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declare pointer ip = new int; // ip points to integer var l note that the variable has no name and the only way to access the variable is though as pointer: cin >> *ip;*ip += 20;cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the var delete - releases memory back to heap so that it can be reused delete ip; note - the memory pointed to by ip goes away not the pointer variable itself l memory allocation types n static variable/constant – location known at compile time: literal constants, global variables, allocated in special place for static vars. n automatic variable – with scope: local variables, parameters, temp. variables, allocated on stack dynamic variable - created with new, allocated in heap 4

5 Memory Leak Problem l note that the pointer that points to the dynamic variable is the only way to access this variable l if the pointer is reassigned the dynamic variable is “lost”. This is called a memory leak problem int *ptr = new int; ptr = new int; // error - memory leak l is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int; l does this code have a problem? int *ptr = new int; delete ptr; *ptr=5; 5

6 Pointers and Arrays l array name is equivalent to: base_type * const l that is array name is a pointer that cannot be changed l array name points to the first element of the array l array name can be used as pointer l pointer can be used as an array name int a[10], *p1, *p2; p1=a; // where does p1 point now? p1[2]=5; // which element does p1 access a=p2; // is this legal? p1[20]; // is this legal? 6

7 Dynamic Arrays l arrays can be created dynamically just as scalar variables. new has to be passed the number of elements of the array int *p1, *p2, num; p1=new int[10]; l the number of array elements need not be constant: cin >> num; p2=new int[num]; p2= new int[0]; // operates correctly, sometimes useful new returns the pointer to the first element of the dynamically allocated array. This pointer can be used just like regular array name: p1[2]=42; l note, unlike regular array name, this pointer can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1; note that the pointer passed to delete does not have to be the same that receives the value returned by new n how does the computer know how many elements to delete? – part of dynamic array implementation 7

8 Pointers and Functions l Pointers can be passed as parameters to and returned by functions // passing pointer by value void funcVal(int *p){ *p = 22; p = new int(33); } // passing pointer by reference void funcRef(int *& p){ *p = 44; p = new int(55); } // passing pointer to pointer, superseded by above: seldom used void funcRefRef(int **pp){ **pp = 66; *pp = new int(77); } // returning pointer int *funcRet(){ int *tmp = new int(88); return tmp; } 8

9 Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i = 0; i < 5; ++i) p[i] = i; p = new int [5]; p 01234 ————— these locations cannot be accessed by program p 01234 9

10 Dynamic Objects l objects can be allocated dynamically class myclass { public: myclass(int n){data=n;} int getdata() const {return data;} private: int data; }; the object of class myclass can be allocated as follows: myclass *mp1; mp1 = new myclass(5); l when new is passed parameter - constructor is invoked l the object can then be used as regular object: cout getdata(); l and destroyed: delete mp1; 10

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

12 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 12

13 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) 13

14 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); 14

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

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

17 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 n seldom used in C++, more common in pointerless-languages (Java, C#) 17

18 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 18

19 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 does one access a dynamically allocated variable? How does one assign a value to such variable at declaration time? l What is memory leak? 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? 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? 19


Download ppt "Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference."

Similar presentations


Ads by Google