11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

376 pointers <strong>and</strong> indirectionA pointer is a variable whose value is an address location. HereMyPtr holds the address 101.second line above, therefore, MyPtr is a pointer to an integervariable. This means that the address <strong>of</strong> any integer variablecan be stored in MyPtr. The last line uses the & (ampers<strong>and</strong>)to represent the address <strong>of</strong> the variable MyVar. Therefore, itstores that address in MyPtr.Examining the lines above, one sees that the variableMyVar has the value 10. The pointer variable MyPtr has thevalue <strong>of</strong> whatever machine address contains the contents<strong>of</strong> the variable MyVar. In an expression, putting an asteriskin front <strong>of</strong> a pointer name “dereferences” the pointer. Thismeans that it returns not the address stored in the pointer,but the value stored at the address stored in the pointer (seethe diagram). Therefore if one writes:AnotherVar = * MyPtr;What is the value <strong>of</strong> AnotherVar? The answer is thecurrent value <strong>of</strong> MyVar (whose address had been stored inMyPtr)—that value, as assigned earlier, is 10.The general concept <strong>of</strong> storing the address <strong>of</strong> anothervariable in a variable is called indirection, or indirectaddressing. It was first used in assembly language to workwith index registers—special memory locations in a processorthat store memory addresses.Uses for PointersAlthough the concept may seem esoteric, pointers have anumber <strong>of</strong> uses. For example, suppose one has a buffer(perhaps storing video graphics data) <strong>and</strong> one wants tocopy it from one area to another. One could declare the bufferto be an array (see array) <strong>and</strong> then reference each element,or memory location <strong>and</strong> copy it. However, this wouldbe rather awkward. Instead, one can declare a pointer, set itto the starting address <strong>of</strong> the buffer, <strong>and</strong> then simply use aloop to increment the pointer, pointing in turn to each locationin the buffer.A similar approach applies to strings in C <strong>and</strong> relatedlanguages. A string <strong>of</strong> characters in C is declared as anarray <strong>of</strong> char. In an array, the name <strong>of</strong> the array is actuallya pointer to the first data location. It is therefore easy tomanipulate strings by getting their starting address by referencingthe name <strong>and</strong> then using one or more pointers tostep through the data locations. For example, the followingfunction copies the contents <strong>of</strong> one string into another:strcpy(char *s1,char *s2){while (*s2)*s1++ = *s2++;}The function takes two strings, s1 <strong>and</strong> s2, declared aspointers to char. It then steps (increments) them (usingthe ++ operator) so that the value in each location in s2 iscopied into the corresponding location in s1. The loop exitswhen the value at s2 is 0 (null), indicating that the end <strong>of</strong>string marker has been reached.Another common use for pointers is in memory allocation.Typically, a program requests memory by giving thememory allocation function a pointer <strong>and</strong> the amount <strong>of</strong>memory requested. The function allocates the memory <strong>and</strong>then returns the starting address <strong>of</strong> the new memory in thepointer, so the program knows how to access that memory.Pointers are also useful for passing a “bulky” variablesuch as a data record to a procedure or function. Suppose,for example, a program needs to pass a 65,000 byte recordto a procedure for printing a report. If it passes the actualrecord, the system has to make a copy <strong>of</strong> the whole record,tying up memory. If, instead, a pointer to the record is used,only the address is passed. The procedure can then accessthe record at that address without having to make a copy.In C <strong>and</strong> some other languages it is even possible tohave a pointer that points to another pointer. A commoncase is an array <strong>of</strong> strings, such asChar Form [80] [20];representing a form that has 20 lines <strong>of</strong> 80 characters. Eachline is an array <strong>of</strong> characters <strong>and</strong> the form as a whole is thus“an array <strong>of</strong> arrays <strong>of</strong> characters.” Therefore, to dereference(get the value <strong>of</strong>) a character one would first dereferencethe line, <strong>and</strong> then the column.Problems with PointersPointers may be useful, but they are also prone to causingprogramming problems. The simplest one is failing todistinguish between a pointer <strong>and</strong> its value. For example,suppose one writes:Total = Total + MyPtr;intending to add the value <strong>of</strong> the variable pointed to byMyPtr to Total. Unfortunately, the asterisk (dereferencingoperator) has been inadvertently omitted, so what getsadded to Total is the machine address stored in MyPtr!Another problem comes when a pointer is used to allocatememory, the memory is later deallocated, but thepointer is left pointing to it.Because pointers can potentially access any location inmemory (or at least attempt to), some computer scientistsview them as more dangerous than useful. It’s true thatmost things one might want to do with a pointer can beaccomplished by alternative means. One attempt to tamepointers is found in C++, which <strong>of</strong>fers the “reference” datatype. A reference is essentially a constant pointer that onceassigned to a variable always dereferences that variable <strong>and</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!