Pointer (computer science)
In computer science, a pointer is the memory address of some other data. More commonly, the term 'pointer' refers to the variable which contains said memory address. Pointers exist at the instruction level of all architectures, and may also exist at the syntactic level of high-level languages (HLLs).
Operations on pointers
A pointer to a memory address A is said to reference A. To manipulate the memory referenced by a pointer P is called dereferencing P. A fundamental strength of pointers is that they can be redefined; a single name can refer to on of many objects. Pointers may also be incremented or decremented, which means adjusted to reference the next or previous object, assuming that objects are placed one after another in memory.
The role of pointers in data structures
The name data structure is quite apt; indeed, they bear quite a resemblance to physical structures. Although there is a large variety in how data structures work, one can say in general that they are composed of several pieces (or objects) which are connected to one another (via pointers). Because pointers may be redefined, they are the fundamentally mutability that allows for a data structure to change over time.
Comparison of pointers in high-level languages
The methods by which a language allows the programmer to manipulate pointers greatly affects the style in which programs are written in that language. Some languages, such as C allow for nearly unrestricted manipulation of pointers, and so programs written in C often rely heavily on pointers, pointer arithmetic, and pointer typecasting. On the other extreme, in languages such as the Java platform or C#, all variables of object type are actually implemented as pointers, though programmers notice no syntactic distinction. Somewhere in the middle of the spectrum lay languages such as Standard ML employ a form of pointer with restricted access, so that the compiler may still prove polymorphic type correctness at compile time. In Standard ML, the majority of data structures are designed to produce modified copies of data structures instead of modifying the data structure in-place with a pointer redefinition.
In languages with unrestricted pointer manipulation, pointers can be created to reference any value. This allows the programmer to maintain reference to memory that has been deallocated, and can be unsafe. Such languages often allow the programmer to perform various arithmetic operations on pointers, such as addition and subtraction, which again can be unsafe, because it is difficult for even the most advanced compilers to assure that the resulting pointer references a valid memory location. Additionally, these languages will often allow the programmer to perform a reinterpret typecast on pointers; in other words, allow the programmer to treat a pointer to an object of type O as an object of type P. This can be unsafe, since the operations that are valid for objects of type O are (unless specially crafted) invalid for objects of type P. Overall, such languages introduce very few restrictions on programmers, an thus enable are large class of program bugs that cannot be detected at compile time. Some argue that this leads to less reliable software and extended development time.
Languages with many restrictions on pointer manipulation are generally agreed to be safer, since the programmer is left with fewer opportunities for error. Additionally, languages in this category generally also feature automatic memory management (garbage collection) to further simplify memory issues. References are explicitly created for every created object and stored into typed pointers. In these languages, pointers cannot be incremented or decremented, since it is a large assumption indeed that objects are stored at sequential memory addresses. Finally, typecasts on pointers in these languages are restricted, so that impossible casts can be rejected at compile time, and other casts can be checked for type compatibility at run time.
- Reference needed
- Reference needed