Pointers Revisited l l l What is variable

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

Dynamic Memory Allocation when need memory at run-time

Dynamic Memory Allocation l l l memory assignment operations n allocation – assignment (claim) of memory for a particular variable n deallocation – release (freeing) of memory usually, memory is allocated at variable declaration memory allocation at variable declaration may not be possible or efficient n ex: manipulating an array of arbitrary size – what if we allocate an array too small? – what if we allocate an array too large? dynamic memory allocation – memory allocation as needed during computation n more flexible since – the program can allocate and release memory once data size is known and potentially can get as much memory as the computer resources allow – may deallocate once no longer needed heap - the system data structure for dynamic memory allocation n what is program (or call) stack again? n heap, like program stack, is separate for every program and is removed when the program finishes 3

new and delete l l l new and delete - operations used for dynamic memory allocation new – allocates a nameless variable of specified type (or class) and returns its address int *ip; // declares pointer ip = new int; // ip points to integer variable this variable is dynamic variable has no name and the only way to access it is though a pointer: cin >> *ip; *ip += 20; cout << *ip; new may take a parameter to initialize the dynamic variable with ip = new int(5); // 5 assigned to the var delete – deallocates variable pointed to by operand delete ip; n the memory pointed to by ip is deallocated, not the pointer variable itself 4

Memory Allocation Types l static variable/constant – location known at compile time n literal constants, global variables, static variables, n allocated in special place for static vars n allocated when program starts execution, deallocated after program ends l automatic variable – with scope n local variables, parameters, temp. variables, n allocated on program stack n allocated when function is invoked, deallocated when function ends l dynamic variable – programmer controlled n allocation managed with new/delete allocated in heap n allocated/deallocated as specified by programmer n 5

Memory Leak Problem l l l pointer that points to a dynamic variable is the only way to access this variable if the pointer is reassigned the dynamic variable may not be accessed. This is the memory leak problem does not cause immediate program crash but wastes computer resources and may lead to resource exhaustion in long-running programs example int *ptr = new int; // error - memory leak is this a memory leak? int *ptr 1, *ptr 2 = new int; ptr 1=ptr 2; ptr 2 = new int; does this code have a problem? int *ptr = new int; delete ptr; *ptr=5; 6

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

Dynamic Arrays l l l arrays can be created dynamically just as scalar variables. new has to be passed the number of elements of the array int *p 1, *p 2, num; p 1=new int[10]; the number of array elements need not be constant: cin >> num; p 2=new int[num]; p 2= new int[0]; // operates correctly, sometimes useful new returns the address of the first element of the dynamically allocated array when assigned to pointer, his pointer can be used just like regular array name: p 1[2]=42; n unlike regular array name, this pointer is not constant and can still be changed p 1=p 2; delete has special syntax for array deallocation: delete [] p 1; the pointer passed to delete does not have to be the same that receives the value returned by new n l l n how does the computer know how many elements to delete? – part of dynamic array implementation 8
![Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i](http://slidetodoc.com/presentation_image_h/21d4242e089ef12a2b26398158f70a3c/image-9.jpg)
Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i = 0; i < 5; ++i) p[i] = i; p p = new int [5]; p 0 1 2 3 4 these locations cannot be accessed by program 0 1 2 3 4 — — — 9

Pointers and Functions // passing pointer by value void func. Val(int *p){ *p = 22; p = new int(33); } pointers can be passed as parameters to and returned by functions // passing pointer by reference void func. Ref(int *& p){ *p = 44; p = new int(55); } // passing pointer to pointer, superseded by above: seldom used void func. Ref(int **pp){ **pp = 66; *pp = new int(77); } // returning pointer int *func. Ret(){ int *tmp = new int(88); return tmp; } 10

Dynamic Objects l l 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 *mp 1; mp 1 = new myclass(5); when new is passed parameter - constructor is invoked the object can then be used as regular object: cout << mp 1 ->getdata(); and destroyed: delete mp 1; 11
- Slides: 11