Variable/fr

From Free Pascal wiki
Jump to navigationJump to search

English (en) suomi (fi) français (fr) русский (ru)

Une variable est une identificateur associé à un bout de mémoire qui peut être inspecté et manipulé durant la durée d'exécution en conformité avec un type de donnée.

Déclaration

Les variables sont déclarées dans une section var.

En Pascal, chaque variable a un type de donnée déjà connu au moment de la compilation (et laissons tomber le type de donnée variant). Une variable est déclarée par un t-uple [math]\displaystyle{ (\text{identifier}, \text{data type identifier}) }[/math], séparé par un deux-points (:) :

var
	foo: char;

Selon les exigences d'espace du type de donnée, la quantité de mémoire appropriée est réservée sur la pile, aussitôt que l'on est entré dans la portée. L'emplacement de la section var détermine sui l'on parle de variables globales ou locales.

Manipulation

Les variables sont manipulées par l'opérateur d'affectation :=. De plus, une série de procédures intégrées attribuent implicitement des valeurs à une variable :

  • routines d'entrée/sortie comme get et put, read et readLn, assign et close
  • new et dispose lors de la manipulation des pointeurs vers des objets
  • setLength lors de la manipulation des tableaux dynamiques.

Définition

In Extended Pascal a variable can be defined, that means declared and initialized, in one term by doing the following.

var
	x: integer value 42;

The FPC as of version 3.2.0 does only support Borland Delphi’s = notation:

var
	x: integer = 42;

The VAX Pascal notation using := is not supported at all.

Accès

A variable is accessed, that means the value at the referenced memory position is read, by simply specifying its identifier (wherever an expression is expected).

Note, there are a couple data types which are in fact pointers, but are automatically de-referenced, including but not limited to classes, dynamic arrays and ANSI strings. With {$modeSwitch autoDeref+} (not recommended) also typed pointers are silently de-referenced without the ^ (hat symbol) being present. This means, you do not necessarily operate on the actual memory block the variable is genuinely associated with, but somewhere else.

Usually the variable’s memory chunk is interpreted according to its data type as it was declared as. With typecasts the interpretation of a given variable’s memory block can be altered (per expression).

Alias de mémoire

In conjunction with the keyword absolute an identifier can be associated with a previously reserved blob of memory. While a plain [math]\displaystyle{ (\text{identifier}, \text{data type}) }[/math] tuple actually sets a certain amount of memory aside, the following declaration of c does not occupy any additional space, but links the identifier c with the memory block that has been reserved for x:

var
	x: byte;
	c: char absolute x;

Here, the memory alias was used as a, one of many, strategies to convince the compiler to allow operations valid for the char type while the underlying memory was originally reserved for a byte. This feature has to be chosen wisely. It necessarily requires knowledge of data type’s memory structure, if nothing is supposed to trigger any sort of access violations.

Most importantly, the additionally referenced memory will be treated as if it was declared regularly. No questions asked.

Voir aussi