Sie sind auf Seite 1von 5

Dr.S.J.M.

Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-1

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-2

Pointer A pointer is a variable that is used to store a memory address (of variable, array, function, class object etc.). Recall that we use memory bins to store different type of data. And the size of a bin (in terms of bits or bytes) depends on the type of data. When we declare a variable of a particular type (say double length), it reserves a memory bin of that type and assigns the variable name to that bin. A memory bin has an address and is usually expressed in hexadecimal. We can store the address of a memory bin (i.e. of a variable name) in a pointer. Then we can access that bin by the pointer as well as by the variable name. num int num; reserves a memory bin of type int and assign its name as num Address of the memeory bin Declaring a pointer long* pnumber; long *pnumber; int *pa, *pb; double *pmark;
//declares pnumber of type pointer to long

The address of operator & The address-of operator & is a unary operator that obtains the address of a variable in the memory. int *pnumber; pnumber=&number;
pnumber.

//&number means the address of number and it is stored in

0012FF7C

// Program - P41 y x #include<iostream> using namespace std; 0012AA14 00FFAB1A void main( ){ px int x, y; py int *px; x int *py; 10 x=10; 0012AA14 px px=&x; 00FFAB1A py py=&y; y=*px; cout<<" x ="<<x<<" y ="<<y<<endl; cout<<"\n *px ="<<*px<<" py ="<<*py<<endl; }

Note : The asterisk may be attached to either type name or variable name.

int* pnumber, number; Indirection operator *

//declare pnumber as pointer to int and number as int

When we put * immediately before a pointer, (in statements other then declaration) it refers to the contents of the memory bin pointed to by the pointer variable. Thus if px is a pointer then *px (in statements other then declaration) means the value stored in the address referred to by px.
Note : By now it can be noticed that the same symbol * has different meaning. It is used as multiplication operator, as indirection operator and also to declare a pointer. The compiler is able to distinguish the meaning by the context.

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-3

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-4

Initializing pointers It is a good practice to initialize pointers (initializing variables also). // Program - P40 #include<iostream> using namespace std; void main( ){ int number; cin>>number; cout<<"&number = "<<&number<<endl; int* pnumber; pnumber=&number; cout<<"pnumber = "<<pnumber<<endl; } It is easy to initialize a pointer with the address of a variable. However, the variable must have to be declared prior to the pointer declaration. int age = 0; // initialized integer variable int* page = &age; // initialized pointer We can also initialize a pointer as below int* pnum=0; // pointer not pointing to anything A pointer initialized in this way is called a null pointer. Array and pointers The array name and a pointer have similarity in that both contain an address. We have seen it before (when we studied array and function, Program P37b) that an array name contains the address of the first element of the array. However, most significant difference between a pointer and an array name is that you can modify the address stored in a pointer, while the address that an array name refers to is fixed. double value[5]; double* pvalue = value; Why pointers are used? There are several reasons some of which are : x pointers can be used to operate on data stored in an array which often executes faster than if array notation is used. x pointers can be used to access within a function blocks of data such as array which is defined outside the function. x space for new variables can be allocated dynamically (i.e. during the program execution) by use of pointers.
// stores the address of the array values in pvalues

Now pvalue+1 will mean the address of value[1] pvalue+2 will mean the address of value[2] pvalue+3 will mean the address of value[3] pvalue+4 will mean the address of value[4] pvalue++; or pvalue+=1; are also valid statements. This is demonstrated by program P43. We have seen by program P03 that memory bins of type double are 8 byte. So if the address of value[0] is 00FF1258 then the address of value[1] will be 00FF1258+8=0012FF1260 value[2] will be 00FF1260+8=0012FF1268 value[3] will be 00FF1268+8=0012FF1270 value[4] will be 00FF1270+8=0012FF1278 thats what we have got in the output of P43.

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-5

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-6

// Program - P43 #include<iostream> using namespace std; void main( ) { double value[5], *pvalue; pvalue=value; cout<<"\n pvalue = "<<pvalue; cout<<"\n value[0]="<<&value[0]; cout<<"\n pvalue+1 ="<<pvalue+1; cout<<"\n value[1]="<<&value[1]; cout<<"\n pvalue+2 ="<<pvalue+2; cout<<"\n value[2]="<<&value[2]; cout<<"\n pvalue+3 ="<<pvalue+3; cout<<"\n value[3]="<<&value[3]; cout<<"\n pvalue+4 ="<<pvalue+4; cout<<"\n value[4]="<<&value[4]; cout<<endl; }

We can also use the name of an array as though it was a pointer. We can address the elements of an array (declared above). *(value+i) refers to value[i] of the array // Program - P44 /* Program to find average of a set of numbers in an array */ #include<iostream> #include<iomanip> using namespace std; void main( ){ double number[10], sum=0.0; int n; cout<<" How many numbers (max 10)? "; cin>>n; cout<<" Input the numbers\n "; for(int i=0; i<n; i++){cin>>number[i];} for(i=0; i<n; i++){sum += *(number+i);} // for(i=0; i<n; i++){sum += number[i];} cout<<"Average = "<<sum/n<<endl; } for a two dimensional array value[ ][ ], *(*(value+i)+j) refers to the element value[i][j]; This can be explained as below value refers to the address of the first row of the array value+i refers to the address of row i of the array *(value+i) refers to the address of the first element of row i *(value+i)+j refers to the address of offset j at row i

double value[5]; value[0] 0012FF58 0012FF60 0012FF68 0012FF70 0012FF78 value[1] value[2] value[3] value[4]

address

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-7

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-8

// Program - P45 #include<iostream> #include<iomanip> using namespace std; void main( ){ double value[10][10]; int m,n; cout<<" No. of rows (max 10)? "; cin>>m; cout<<" No. of columns (max 10)? "; cin>>n; cout<<" Input the elements of the matrix (row wise)\n "; int i,j; for(i=0; i<m; i++){ for(j=0; j<n; j++)cin>>value[i][j];} for(i=0; i<m; i++){ for(j=0; j<n; j++)cout<<setw(7)<<value[i][j]; cout<<endl;} cout<<"value[1][2] = "<<value[1][2]<<endl; cout<<"*(*(value+1)+2)= "<<*(*(value+1)+2)<<endl; }

Dynamic memory allocation When we declare a variable or an array in the source code in the form int salary; float area; string address; double ce206[50]; the corresponding memory requirement is decided at compile time. All of this amount of memory will be allocated, at execution of the program, whether we need them or not. // Program - P44a /* Program to find average of a set of numbers in an array */ #include<iostream> #include<iomanip> using namespace std; void main( ){ double number[10], sum=0.0; int n; cout<<" How many numbers (max 10)? "; cin>>n; cout<<" Input the numbers\n "; for(int i=0; i<n; i++){cin>>number[i];} for(i=0; i<n; i++){sum += number[i];} cout<<"Average = "<<sum/n<<endl; } In a program P44a we have set the dimension of array number to 10. Now if we prepare the .exe file we wont be able to process more than 10 numbers. We may think that, we will prepare the software with a higher array dimension say 100000. But now, when this program is invoked it will occupy higher amount of memory. (it can be checked invoking And this same higher windows taskmanager and running P44a with different dimension) memory will be occupied even when you process only two numbers as well as when you process 100000 numbers. In a large program, where there will be many such arrays, it may so happen that for some arrays

The above usages of pointer are not necessary. Major and effective use of pointer is for dynamic memory allocation which is shown below.

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-9

Dr.S.J.M.Yasin/CE206_lecture_09_pointer/ 28 March 2009/P-10

the memory is allocated needlessly whereas for some other arrays, there is not enough memory. Such a situation can be avoided by using dynamic memory allocation. Dynamic memory allocation means that the amount of memory to be allocated will be decided at run time. By definition, dynamically allocated variables cannot be declared at compile time and so they cannot be named in the source code. When we run programs, there may be unused memory, in the computer. This unused memory is called free store or heap. We can allocate space within this free store for a new variable by using a special c++ operator new. Also we can de-allocate a previously allocated memory by the operator delete. double *pvalue; pvalue = new double;
remember that it is a good idea to initialize a pointer (same is true for a variable or array) such as double *pvalue = 0;

Program P46 shows the use of dynamic memory allocation. // Program - P46 /* Program to find average of a set of numbers in an array using free store */ #include<iostream> #include<iomanip> using namespace std; void main( ){ int n; cout<<" How many numbers? "; cin>>n; double *number= new double[n]; double sum=0.0; cout<<" Input the numbers\n "; for(int i=0; i<n; i++){cin>>*(number+i);} for(i=0; i<n; i++){sum += *(number+i);} cout<<"Average = "<<sum/n<<endl; delete [] number; }

The first line above is a pointer declaration. The second line of code will allocate a memory bin of type double in the free store and store its address in pointer pvalue. Now we can use this memory bin by statements (using the indirection operator) such as *pvalue = 20.5; When this memory is not required we can de-allocate this memory by delete pvalue; More examples char *pstring; ......................... pstring = new char[20]; ............................... delete [ ] pstring; double *pnum; int max; ............. cin>>max; ............................... pnum = new num[max]; .................................. delete [ ] pnum;

Warning : Memory once allocated by the new operator wont be available (even if it is not required any more) for other variables unless it is de-allocated by delete operator.

Das könnte Ihnen auch gefallen