Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Revisited What is variable address, name, value?

Similar presentations


Presentation on theme: "Pointers Revisited What is variable address, name, value?"— Presentation transcript:

1 Pointers Revisited What is variable address, name, value?
What is a pointer? How is a pointer declared? What is address-of (reference) and dereference 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?

2 Dynamic Memory Allocation

3 Why Dynamic Memory when a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated not all memory can be allocated or efficiently allocated at the compilation time 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? dynamic memory allocation – memory allocation at execution time under the direct control of programmer more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow heap - the system data structure for dynamic memory allocation what is program (or call) stack again? heap, like program stack, is separate for every program and is removed when the program finishes

4 new and delete new and delete - operations used for dynamic memory allocation new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declares pointer ip = new int; // ip points to integer variable note that the variable has no name and the only way to access the variable is though a 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 for reuse delete ip; note: the memory pointed to by ip is deallocated, not the pointer variable itself memory allocation types static variable/constant – location known at compile time: literal constants, global variables, static variables, allocated in special place for static vars. automatic variable – with scope: local variables, parameters, temp. variables, allocated on program stack dynamic variable - created with new, allocated in heap

5 Memory Leak Problem pointer that points to the 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 int *ptr = new int; ptr = new int; // error - memory leak is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int; does this code have a problem? delete ptr; *ptr=5;

6 Pointers and Arrays 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 array name can be used as pointer 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?

7 Dynamic Arrays 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]; 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; unlike regular array name, this pointer is not constant and can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1; the pointer passed to delete does not have to be the same that receives the value returned by new 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 = 0; i < 5; ++i) p[i] = i; p = new int [5]; p 1 2 3 4 p 1 2 3 4 these locations cannot be accessed by program

9 Pointers and 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; pointers can be passed as parameters to and returned by functions

10 Dynamic Objects 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); when new is passed parameter - constructor is invoked the object can then be used as regular object: cout << mp1->getdata(); and destroyed: delete mp1;


Download ppt "Pointers Revisited What is variable address, name, value?"

Similar presentations


Ads by Google