Information AboutPointer |
|
In Computer Science , a pointer is a Programming Language Data Type whose value refers directly to (or “points to”) another value stored elsewhere in the Computer Memory using its Address . Obtaining the value to which a pointer refers is called '''dereferencing''' the pointer. A pointer is a simple implementation of the general Reference data type (although it is quite different from the facility referred to as a '' Reference '' in C++). Pointers are so commonly used as references that sometimes people use the word “pointer” to refer to references in general; however, more properly it only applies to data structures whose interface explicitly allows it to be manipulated as a memory address. If you are seeking general information on a small piece of data used to find an object, see Reference (computer Science) . POINTERS IN DATA STRUCTURES When setting up Data Structures like Lists , Queues and trees, it is necessary to have pointers to help manage the way in which the structure is implemented and controlled. Typical examples of pointers would be start pointers, end pointers, or stack pointers. ARCHITECTURAL ROOTS Pointers are a very thin Abstraction on top of the addressing capabilities provided by most modern Architecture s. In the simplest scheme, an '' Address '', or a numeric Index , is assigned to each unit of memory in the system, where the unit is typically either a Byte or a Word , effectively transforming all of memory into a very large Array . Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address. 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 possibility that a program may attempt to access an address which corresponds to no unit of memory, either because not enough memory is installed or due to the architecture does not support such addresses. The first case may, in certain platforms as the Intel X86 architecture, be called a Segmentation Fault (segfault). The second case is possible in the current implementation of AMD64 , where pointers are 64 bit long and addresses only extend to 48 bits. There, pointers must conform to certain rules (canonical addresses), so if a noncanonical pointer is dereferenced, the processor raises a General Protection Fault . On the other hand, some systems have more units of memory than there are addresses. In this case, a more complex scheme such as Memory Segmentation or Paging is 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 the PAE paging mechanism. Thus, only 1/16 of the possible total memory may be accessed at a time. Another example in the same computer family was the 16-bit Protected Mode of the ancient 80286 processor, which, though supporting only 16 MiB of physical memory, could access up to 1 GiB of virtual memory, but the combination 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 architectures provide Memory-mapped I/O , which allows some addresses to refer to units of memory while others refer to Device Register s of other devices in the computer. There are analogous 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. USES Pointers are directly supported without restrictions in languages such as C , C++ , Pascal and most Assembly Language s. They are primarily used for constructing Reference s, which in turn are fundamental to constructing nearly all Data Structure s, as well as in passing data between different parts of a program. In functional programming languages that rely heavily on lists, pointers and references are managed abstractly by the language using internal constructs like Cons . When dealing with Array s, the critical lookup operation typically involves a stage called ''address calculation'' which involves constructing a pointer to the desired data element in the array. In other data structures, such as linked lists, pointers are used as references to explicitly tie one piece of the structure to another. Pointers are used to pass parameters by reference. This is useful if we want a function's modifications to a parameter to be visible to the function's caller. This is also useful for returning multiple values from a function. C pointers The basic syntax to define a pointer is
This declares money as a pointer to an integer.Since the contents of memory are not guaranteed to be of any specific value in C, care must be taken to ensure that the address that money points to is valid.This is why it is suggested to initialize the pointer to NULL
If a NULL pointer is dereferenced then a runtime error will occur and execution will stop likely with a segmentation fault. Once a pointer has been declared then, perhaps, the next logical step is to point it at something int a = 5;
money = &a; This assigns the value of money to be the address of a.For example, if a is stored at memory location of 0x8130 then the value of money will be 0x8130 after the assignment.To dereference the pointer, an asterisk is used again
This says to take the contents of money (which is 0x8130), go to that address in memory and set its value to 8.If a were then accessed then its value will be 8.This example may be more clear if memory were examined directly. Assume that a is located at address 0x8130 in memory and money at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits wide.The following is what would be in memory after the following code snippet were executed int a = 5;
(The NULL pointer shown here is 0x00000000.) By assigning the address of a to moneymoney = &a; yields the following memory values Then by dereferencing money by doing
the computer will take the contents of money (which is 0x8130), go to that address, and assign 8 to that location yielding the following memory.Clearly, accessing a will yield the value of 8 because the previous instruction modified the contents of a by way of the pointer money.C arrays Taking C pointers to the next step is the array.
This allocates a block of five integers and declares array as a pointer to this block. Another common use of pointers is to point to dynamically allocated memory from Malloc which returns a consecutive block of memory of no less than the requested size that can be used as an array.
Default values of an array can be declared like: int array {Link without Title} = {2,4,3,1,5}; If you assume that array is located in memory starting at address 0x1000 on a 32-bit Little-endian machine then memory will contain the following:Representing here are five integers: 2, 4, 3, 1, and 5. These five integers occupy 32 bits (4 bytes) each with the least-significant byte stored first (this is a little-endian architecture) and are stored consecutively starting at address 0x1000. The syntax for C with pointers is:
The last example is how to access the contents of array. Breaking it down:
C linked list Below is an example of the definition of a Linked List in C.
#define EMPTY_LIST NULL struct link {
}; Note that this pointer-recursive definition is essentially the same as the reference-recursive definition from the Haskell Programming Language : data Link a = Nil |
|
|