A Pointer Is a Variable That Is Used to Store a Memory Address (Of Variable, Array, Function
Total Page:16
File Type:pdf, Size:1020Kb
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 any type of data. And the size of the bin (in terms of bits or bytes) depend 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 reserves a memory bin of type int int num; and assign its name as num 0012FF7C
Address of the memeory bin
Declaring a pointer
long* pnumber; //declares pnumber of type pointer to long long *pnumber; int *pa, *pb; double *pmark;
Note : The asterisk may be attached to either type name or variable name.
int* pnumber, number; //declare pnumber as pointer to int and number as int
Indirection operator *
When we put * immediately before a pointer, 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. 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; //&number means the address of number and it is stored in pnumber. // Program - P41 y #include Why use pointers? There are several reasons some of which are : pointers can be used to operate on data stored in an array which often executes faster than if array notation is used. pointers can be used to access within a function blocks of data such as array which is defined outside the function. space for new variables can be allocated dynamically (i.e. during the program execution) by use of pointers. Initializing pointers It is a good practice to initialize pointers (initializing variables also). It is easy to initialize a pointer to 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 has similarity in that both contains 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 the array name refers to is fixed. double value[5]; double* pvalue = value; // 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 that’s what we have got in the output of P43. // Program - P43 #include double value[5]; value[0] value[1] value[2] value[3] value[4] 0012FF58 0012FF60 0012FF60 0012FF60 0012FF60 address 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 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 In a program P44a we have set the dimension of array number to 10. Now if we prepare the .exe file we won’t 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 windows taskmanager and running P44a with different dimension) And this same higher memory will be occupied even when you process 2 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 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. remember that it is a good idea to initialize a double *pvalue; pointer (same is true for a variable or array) such pvalue = new double; as double *pvalue = 0; 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; double *pnum; More examples int max; ...... char *pstring; cin>>max; ...... pstring = new char[20]; pnum = new num[max]; ...... delete [ ] pstring; delete [ ] pnum; Warning : Memory once allocated by the new operator won’t be available (even if it is not required any more) for other variables unless it is de-allocated by delete operator. 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 Variable of type long 1000 1004 1008 15467 1000 A pointer Variable