In Computer Science Computer Science, a Pointer Is a Programming
Total Page:16
File Type:pdf, Size:1020Kb
InIn computercomputer science,,aa pointer is aa programmingprogramming language datadata type whosewhose value refers directly to (or "points to") another value stored elsewhere in thethe computecomputerr memory usingusing its address..ForFor high-levelhigh-level programming languages,,pointerspointers effectively take the place of of generalgeneral purpose registers inin low-level languages such asas assemblyassembly language oror machinemachine code,,butbut may be in availableavailable memorymemory..AA pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing the pointer. A pointer is a simple, more concrete implementation of the more ababstract stract reference datadata type. Several languages support some type of pointer, although some have more restrictions on their use than others. As an analogy, a page number in a book could be considered a pointer to the corresponding pagepage;; dereferencing such a pointer would be done by fliflippingpping to the page with the given page number. Pointers to data significantly improve performance for repetitive operations such as traversingtraversing stringsstrings,, lookuplookup tables,, controlcontrol tables andand treetree structures.structures. In particular, it is often much cheaper in time and space to copy and dereferedereferencence pointers than it iiss ttoo copy and access the data to whiPointers are also used to hold the addresses of entry points forfor calledcalled subroutines inin proceduralprocedural programming andand forfor run-timerun-time linking to dynamic link libraries (DLLs)..InIn object-orientedobject-oriented programming,, pointerspointers to functions areare used forfor bindingbinding methodsmethods,, often using what are calledcalled virtualvirtual method tables.. While "pointer" has been used to refer to references in general, it more properly applies to data structures whose interface explicitly allows the pointer to be manipulated (arithmetically via pointer arithmetic) as a memory address, as opposed to aa magicmagic cookicookiee oror capabilitycapability where this is not possible.[[citation needed ]] BecausBecausee pointers allow both protected and unprotected access to memory addresses, there are risks associated with using them particularly in the latter case. Primitive pointers are often stored in a format similar to anan integerinteger;;however,however, attempting to dereference or "look up" a pointer whose value was never a valid memory address would cause a program to crash. To ameliorate this potential problem, as a matter of type safety,,pointerspointers are considered a separate type to the tytypepe of data they point to, even if the underlying representation is ch the pointers point. Use in data structures When setting upup datadata structures likelike listslists,, queuesqueues andand trees, it is necessary to have pointers to help manage how the structure is implemented and cocontrolled.ntrolled. Typical examples of pointerspointers are start pointers, end pointers, andand stack stack pointers.pointers. These pointers can either be absolute (the actualactual physicalphysical address oror aa virtualvirtual address inin virtualvirtual memory))oror relative (an(an offsetoffset fromfrom an absolute start address ("base") that typically uses fewer bits than a full address, but will usually require one additional arithmetic operation to resolve). A two-byte offset, containing a 1616-bit,-bit, unsigned ininteger,teger, can be used to provide relarelativetive addressing for up to 64 kilobytes of a data structure. This can easily be eextendedxtended to 128K, 256K or 512K if the address pointed to is forced to be on a half-word, word or double-word boundary (but, requiring an additional "shift left" bitwisebitwise operation — by 1,2 or 3 bits — inin order to adjust the offset by a factor of 2,3 or 4, before its addition to the base addresaddress).s). Generally, though, such schemes are a lot of troubltrouble,e, and for convenience to the programmer aa flat address space is preferred. A one byte offset, such as the hexadecimalhexadecimal ASCIIASCII valuevalue of a character (e.g. X'29') can be used to point to an alternative integer value (or index) in an array (e.g. X'01'). In this way, characters can be very efficiently translated from ''rawraw data'' to a usable sequentialsequential indexindex andand then to an absolute address without aa lookuplookup table.. [[editedit] Use in control tables Control tables,,thatthat are used to controlcontrol programprogram flow,,usuallyusually make extensive use of pointers. The pointers, usually embeddeembeddedd in a table entry, mamay,y, for instance, be used to hholdold the entry points toto subroutinessubroutines toto be executed, based on certain conditions defindefineded in the same table entry. The pointers can however be simply indexes ttoo other separate, but associated, tables comprising an array of the actual addresses or the addresses themselves (depending(depending upon the programming language constructs availabavailable).le). They cacann also be used to point ((back)back) to earlier table entries (as in loop processing) or forward to skip some table entries (as in aa switchswitch oror "early" exit from a loop). For this latter purpose, the "pointer" may simply be the tabletable entry number itself and can be transformed into an actual address by simple arithmetic. [[editedit] Architectural roots Pointers are a very thinthin abstractionabstraction onon top of the addressing capabilities provided by mmostost modernmodern architecturesarchitectures..InIn the simplest scheme, anan address,,oror a numericnumeric indexindex,,isis assigned to each unit of memory in the system, where the unit is typically either aa bytebyte oror aa wordword,, effectively transforming all of memory into a very largelarge arrayarray..Then,Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address (usually utilizing the machine'smachine's generalgeneral purpose registers)).. In the usual case, a pointer is large enough to hold more addresses than there are units of memory in the system. This introduces the possibilipossibilityty that a program may attemattemptpt to access an address which corresponds to no unit of memory, either because not enough memory is installed (i.e. beyond the range of available memormemory)y) or the architecture does not supportsupport such addresses. The first case may, in certain platformsplatforms such as thethe IntelIntel x86 architecture,architecture, be called aa segmentationsegmentation fault (segfault).(segfault). The second case is possible in the currentcurrent implementation of of AMD64AMD64,,wherewhere pointers are 64 bit long and addresses only extendextend to 48 bits. There, pointers must conform to certain rules (canonical addresses), so if a noncannoncanonicallonica pointer is dereferenced, the processor raises aa gegeneralneral protection fault.. On the other hand, some systems have more units of memory than there are adaddresses.dresses. In this case, a more complex scheme such asas memorymemory segmentsegmentationation oror pagingpaging isis employed to use different parts of the memory at different times. The last incarnations of the x86 architecture support up to 36 bits of physical memory addresses, which were mapped to the 32-bit linear address space through thethe PAEPAE pagingpaging mechanism. Thus, only 1/16 of the possible ttotalotal memory may be accessed at a time. Another example in the same computer family was the 16-bit16-bit protectedprotected mode ofof thethe 8028680286 processor,processor, which, though supporting only 16 MiB oof f physical memory, could accesaccesss up to 1 GiB of virtual memory, but the combinatiocombinationn of 16-bit address and segment registers made accessing more than 64 KiB in one data structure cumbersome. Some restrictions of ANSI pointer arithmetic may have been due to the segmented memory models of this processor family. In order to provide a consistent interface, some ararchitectureschitectures provideprovide memory-mappedmemory-mapped I/O,, which allows some addresses to refer to units of memory while others refer toto devicedevice registers ofof other devices in the computer. There are analogousanalogous concepts such as file offsets, array indices, and remote object references that serve some of the same purposes as addresses for other types of objects. [[editedit] Uses Pointers are directly supported without restrictions in languages such asas PL/IPL/I,, CC,, C++C++,, PascalPascal,, and mostmost assemblyassembly languages..TheyThey are primarily used for constructingconstructing referencesreferences,,whichwhich in turn are fundamental to constructing nearly allall dadatata structures,,asas well as in passing data between different parts of a program. In functional programming languages that rely heavily on lilists,sts, pointers and references are managed abstractly by the language using internal constructs likelike conscons.. When dealing with arrays, the critcriticalical lookuplookup operationoperation typically involves a stage called address calculation which involves constructing a pointer to the desireddesired data element in the array. If the data elements in the array have lengths that are divisible by powers of two, tthishis arithmetic is usually much moremore efficientefficient..PaddingPadding is frequently used as a mechanism for ensuring this is the case, despite the increased mememorymory