Pointer (programming)

See also: Pointer

A pointer is in programming Variable containing an address memory.

Utility

The concept of pointer reflects the different use which one can make of a integer, namely to indicate a addresses memory. This use is very different from a use Arithmétique, from where the creation of registers specific processors (address registers) and of a specific type of data in the computer programming languages.

The pointers " typés" , the most widespread type of pointer currently, appeared with the language C, which added a typing on the data pointed by the pointer. When such a pointer is incremented, it is not in fact inevitably not incremented of one, but of the size of the pointed type.

The use of the pointers makes it possible to have access to the low layer of the Ordinateur, an direct access with the memory. One can literally move of box memory out of box memory. This technique makes it possible to carry out optimizations on the core use or the performance in term speed.

The pointers are amongst other things used to store the addresses of the zones memories allocated dynamically by the application. If a block of size T is allocated and that one receives the address has in return, that wants to say that the block occupies the memory starting from the address has to address A+T-1.

In the languages moreover high level, the use of the pointers is removed, with the profit of the references and the dynamic tables managed by the Compilateur. The references fulfill certain functions of the pointers by removing the access to the memory. That avoids many problems, n the other hand certain uses and optimizations are not possible any more.

Complexification

The use of the pointers is very powerful in certain languages. This power and especially the fact that one touches directly with the memory without any control, complexes the development of an application.

If attention is not paid and that one reaches a zone memory which is not allocated to us, the Processeur via the Operating system will generate a Erreur of segmentation which will even cause a Exception will make plant the application. Moreover, as the memory allocations are carried out partly by the developer, it must be also given the responsability with the release of the memory when it does not need any more, to the risk to see an escape memory appearing.

All these disadvantages oblige the developer to deal with additional things, thus complexing the application and being able to add bugs.

Languages using the pointers

Pointers out of C and C++

The two principal languages using the pointers enormously are the C and the C++. In these languages, a pointer is declared with a star “*”. One recovers the address of a variable with a and commercial “&”.

The star, appointed, is used also for déréférencer the address of a pointer. In other words to have access to the contents pointed by the pointer.

Here a small example: int has = 2; //D eclaration of the variable has of int type (whole) and initialized with 2 int * p = NO ONE; //D eclaration of a pointer on a int which is worth NO ONE p = &a; //l ' addresses of has is affected with the pointer p

  • 5; //D ereferenciation of p, to assign 5 to the variable has

Pointers in a PASCAL

Syntax in Pascal is very similar to that out of C and C++. The typified pointers are declared with a hat “^” appointed with the pointed type. One recovers the address of a variable with a arobase “@”.

The hat is used also for déréférencer the address of a pointer, but then it is placed after. The example above is thus written in a PASCAL as follows: VAr a: integer; {declaration of the variable has of integer type (whole)} p: ^integer; {declaration of the variable p of the pointer type on entirety} begin a:= 2; {initialization of has to 2} p: = @a; {addresses of assigned to the pointer p} p^: = 5; {dereferenciation of p, to assign 5 to the variable has} end;

Types are preset like: type pchar = ^char; pinteger = ^integer;

Arithmetic of the pointers

One calls arithmetic pointers the possibility of making arithmetic operations (incrementing, decrementation, addition and subtraction) on the pointers. That amounts carrying out a displacement in memory.

The characteristic of this use of the pointers, is that one moves by jump of the size memory of the type of data pointed by the pointer and not by byte (which is the smallest accessible unit).

Here is an example out of C: tank = {“, “has you”, “B”, “\ 0”}; tank * p1 = &tab;

Here the pointer p1, contains the address of the first element of the table (what is equivalent to p1 = tab). If one déréférence p1, we would thus have the character “you. p1 = p1 + 1;

By doing that, p1 does not point any more on the character “you, but on “has”. In this example, displacement in memory was of a byte (because the char type is worth always a byte). But if the table had contained data of the long type, displacement would have been of four bytes. Displacement is thus done by jump of the size memory of the type of data pointed by the pointer.

Example in a PASCAL

Once again, syntax in a PASCAL is very close. const {the two definitions are equivalent} : array off tank = 'TAB'#0; tab2: array off tank = (“, “has You”, “B”, #0); VAr p1: ^char; begin p1: = @tab; {p1 point on “You} Inc (p1); {p1 point on “has”} end.

An important difference however remains, it is that one can only increment and décrémenter a typified pointer. To make an addition itself, it is necessary to convert into entirety initially: begin p1: = @tab; {p1 point on “You} p1: = ptr (integer (p1) + 2); {p1 point on “B”} end. In this case, it is necessary to take oneself charges with it the possible multiplication by the size with the type of the pointed data.

Problems of the cast

To make, for C. For C++, the cast are even complicated, with the static_cast, dynamic_cast, reinterpret_cast, and const_cast. -->

Pointer on a function

The pointers can also contain the address of a function. The latter can thus be last in parameter with another function and be called.

Example out of C

Here an example out of C, declaration of a pointer which contains successively the functions fonction_1 () and fonction_2 (): void fonction_1 () { printf (" post fonction_1"); }

void fonction_2 () { printf (" post fonction_2"); }

int hand () { void (*fonction) (); function = fonction_1; function (); function = fonction_2; function (); }

Even example in a PASCAL

procedure fonction_1; begin writeln (“fonction_1 posts”); end; procedure fonction_2; begin writeln (“fonction_2 posts”); end; VAr fonc: procedure; begin {principal block} @fonc: = @fonction_1; fonc; @fonc: = @fonction_2; fonc; end.

See too

Random links:Garlic with round head | World cup of Rugby at XV female 1998 | University of Arm of Apic | Solar filament | Liege-Bastogne-Liege 2007 | Théâtre_d'étoile_(film)