Pointer (computer programming)

With pointer ( also English pointer) is called in computer science, a special value whose meaning is a memory address. At this address data either as variables or objects, as well as program code can be. By dereferencing the pointer, it is possible to access the data or the code.

Pointers are inter alia used to manage dynamic memory. Thus, certain data structures, such as linked lists, typically implemented by means of pointers.

A pointer is a special case and in some programming languages, the only possible implementation of the concept of a reference.

The pointer was 1964/65 introduced by Harold Lawson and implemented in the programming language PL / I.

Pointer in programming languages

Pointers are mainly used in machine-oriented programming languages ​​such as Assembler, C or C before, while the use in strongly typed languages ​​such as Modula -2 or Ada greatly restricts and they both internally present in languages ​​such as Java or Eiffel, but completely hidden from the programmer (opaque ) are. With the first-mentioned languages ​​, it is possible to create pointers to arbitrary locations in memory or to be reckoned with.

Some languages ​​restrict the use of pointers, because programmers when working with pointers undermined slightly serious programming errors (known as a cause of buffer overflows or memory access violations ( SIGSEGVs ) and consequently represent crashes when, for example, in C and C programs written ).

In object- oriented languages, takes the place of the pointer alternatively (C ) or exclusively (Java, Python) the reference that does not have to be explicitly dereferenced unlike pointers.

In the language C # or VB.NET pointers do not exist basically. All functionalities that provide pointers have been replaced by concepts such as Delegate. However, it is in C # but not in VB.NET to declare unsafe code (which must be compiled specifically ) to pointers to how to use C . So that better performance may be achieved in some cases, or it is possible to access the Windows API functions.

Typed pointers

In most high-level programming pointers are directly associated with data types. So can usually refer "pointer to an object of type Integer" only to an object of type "Integer " field. The data type of the pointer itself that is determined by the type, to which it refers. In the C programming language which is a prerequisite for the realization of pointer arithmetic (see below), it is only through the knowledge of the memory size of the associated type, the address of the predecessor or successor element can be calculated. In addition, the typing of pointers allows to recognize the compiler violations of type compatibility.

Untyped pointer

These pointers are not connected to any data type. They can not be dereferenced, incremented or decremented, but must be converted to a typed pointer type before accessing.

Examples of this are the type void * in C and C , Objective- C type id or POINTER in Pascal.

In high-level languages ​​in part, there are no untyped pointer.

Null pointer

The null pointer is a pointer with a special, reserved for this value (so-called zero value, not necessarily numeric 0), which indicates that it refers to nothing. Null pointers are very commonly used in nearly all languages ​​, since you a " designated space" designates by the null pointer. For example, a singly linked list is usually implemented so that the last element points to the null pointer as a result element. It thus indicates, in this case the end of the list. In Pascal and Object Pascal the null pointer means, for example nil ( Latin: " nothing," or an acronym for " not in list "). In the C contained in the Standardbiliothek preprocessor macro NULL denotes the null pointer and hides the internal representation. In C , the null pointer is also called NULL and is defined as a macro for the numerical zero (0). In the new C standard C 11 nullptr constant was introduced, which enables a type-safe distinction between 0 and the null pointer. The zero reference in Python is called None.

The dereferencing a null pointer is generally not allowed. Depending on the programming language, it raises an exception or undefined behavior. On many modern operating systems, the program is terminated with an access violation.

Uninitialized pointer

If a pointer variable is dereferenced does not point to a valid memory area of the corresponding type, it can also cause unexpected behavior. So there may be a situation when a variable has not been initialized before use to a valid value, or if it still refers to a memory address that is not valid more ( wild pointers). Displays the pointer does not point to a valid memory address, it may cause a protection violation as the null pointer.

Pointer operations

  • Dereferencing. on the object to which the pointer points to access. In the case of a function pointer, this will in most cases correspond to an actual call to the referenced program fragment.
  • Increment / decrement. move the pointer to the object that is in memory behind / in front of the original object. Internally, this is implemented by adding or subtracting the object size. This is the compiler only known when the type of the referenced object is clearly identified at compile time. The resulting pointer value can be assigned to the variable containing immediately of course, if there is this. The pre-and post - Inkrementierungsoperatoren in C afford the example.
  • Destroy: . of the referenced object (see constructor / destructor ). It offers itself by calling the destructor, to set all variables that contain pointers to the destroyed object to the zero value, will be needed to realize that not a valid object longer referenced. However, this is not possible in general.
  • Compare: . with other pointers or NULL.

Pointer arithmetic

The increment or decrement a pointer to a fixed value or subtracting two pointers is called pointer arithmetic.

Since these operations are very prone to error, they are not supported in most high-level languages ​​, which of course there are again given other methods to implement the same functionality.

Properties of pointers to data

Benefits

The use of pointers can speed up or help to save space program execution in certain cases:

  • Is that from a program in memory to hold the amount of data on program start is unknown, can accurately be allocated as much memory as is needed ( Dynamic memory management ).
  • It is possible to return back to the operating system during program execution no longer needed memory.
  • With the use of arrays or vectors can jump quickly using pointers within the field and navigate. Using phasor is in this case passed through a field. Instead of an index to be used and to address as the array elements on this, one is at the beginning of the sequence a pointer to the beginning of the array and increments this pointer in each run. This type of access to fields is internally implemented in many languages ​​and compilers, at some points automatically.
  • Refer to the memory areas can be altered, for example, for sorting lists, without copying the elements must (dynamic data structures).
  • For function calls, can be avoided by passing a pointer to an object to pass the object itself, which would require, in certain cases very time consuming to make a copy of the object ( reference parameters).
  • Instead of variables to copy every time and put so again each time space available, you can just have them point to the same variable in some cases multiple pointers.
  • For string memory contents can be addressed directly, without having to go through objects and functions.

Disadvantages and risks

There are languages ​​that deliberately refrain from the use of pointers (see above). This is mainly due to:

  • Dealing with pointers is difficult to learn, difficult and error-prone. To think primarily in terms of pointers, programming beginners often presents difficulties. Even with experienced programmers are careless in dealing with pointers still relatively frequent.
  • In some programming languages, no effective data -type control is possible, that is, when you run can not be controlled, what data to the destination address, and whether these expectations (specifications ) correspond to the program sequence
  • Programming errors when working with pointers can have severe consequences. So it is, for example, program crashes, undetected data corruption ( due to stray pointer ), buffer overflows or "lost" memory regions ( memory leaks ): The program continually requests more memory, the other programs are no longer available, to the extreme case the operating system can no longer provide enough.
  • Contact data structures of pointers together that refer to single small memory blocks, this particular for processes that run very long, leading to the fragmentation of the address space, so that the process can not request a further memory, although the sum of the allocated memory blocks is substantially less than the available memory.
  • The efficiency of the processor cache suffers when a data structure of memory blocks in many points that are far apart in the address space. Therefore, it may be useful to instead use arrays, because they have a compact representation in memory.
  • The latter can also affect negatively related to paging.
  • Last but not least, a pointer is a typical approach instead of malware: This malware just needs a place to change, to point to its own program code: there is no proper control of the area reserved for the program memory area, this may also be arbitrary elsewhere. In addition, buffer overflows are easy to produce on errant pointer. In particular, as lying in variable data program code together for execution. This represents a typical Erstinfektionstechnologie

Smart pointers

As Smart pointers ( smart pointers ) objects are known that encapsulate simple pointer and equip it with additional functions and features. For example, could a " smart pointer " Share a dynamically Allocated memory object when the last reference is deleted it.

Pointer to a COM or CORBA interface are implemented in some programming languages ​​(such as Object Pascal ) and Smart pointers.

Function pointer ( method pointer )

Function pointers are a special class of pointers. Do not point to an area in the data segment, but to the entry point of a function in the code segment of memory. This makes it possible to implement custom function calls whose destination is determined at run time. Function pointers are often used in conjunction with callback functions (callback function) to use and provide a form of late binding dar.

229835
de