Difference between revisions of "Variable/ru"

From Lazarus wiki
Jump to navigationJump to search
Line 3: Line 3:
 
'''Переменные''' - это символьные последовательности, которые определяет программист и для которых выделяется память ([[Integer/ru|integer]], [[Char/ru|char]] и т.д.). Они могут находится в области видимости всей программы ([[Global variables/ru|глобальные переменные]]) или только внутри процедуры, функции, метода ([[Local variables/ru|локальные переменные]]).
 
'''Переменные''' - это символьные последовательности, которые определяет программист и для которых выделяется память ([[Integer/ru|integer]], [[Char/ru|char]] и т.д.). Они могут находится в области видимости всей программы ([[Global variables/ru|глобальные переменные]]) или только внутри процедуры, функции, метода ([[Local variables/ru|локальные переменные]]).
  
== declaration ==
+
== Объявление ==
Variables are declared in a [[Var|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight> section]].
+
Переменные объявляются в секции [[Var|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>]]. В [[Pascal|Паскале]] каждая переменная имеет [[Data type/ru|тип данных]], известный уже на этапе [[Compile time|времени компиляции]] (и пусть это будет тип данных [[Variant|<syntaxhighlight lang="pascal" enclose="none">variant</syntaxhighlight>]]). Переменная объявляется парой <math>(\text{identifier}, \text{data type identifier})</math>, разделенный [[Colon|двоеточием]]:
In [[Pascal]] every variable has a [[Data type|data type]] already known at [[Compile time|compile-time]] (and let it be the [[Variant|data type <syntaxhighlight lang="pascal" enclose="none">variant</syntaxhighlight>]]).
 
A variable is declared by a <math>(\text{identifier}, \text{data type identifier})</math> tuple, separated by a [[Colon|colon]]:
 
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
var
 
var
 
foo: char;
 
foo: char;
 
</syntaxhighlight>
 
</syntaxhighlight>
According to the data type's space requirements, the appropriate amount of memory is reserved on the stack, as soon as the corresponding [[Scope|scope]] is entered.
+
 
Depending on where the <syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-section is placed, you can speak of either [[Global variables|global]] or [[Local variables|local]] variables.
+
В соответствии с требованиями к пространству типа данных соответствующий объем памяти резервируется в стеке, как только вводится соответствующая [[Scope| область видимости переменной]]. В зависимости от того, где находится <syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-секция, вы можете говорить о [[Global_variables/ru|глобальных]] или [[Local_variables/ru|локальных]] переменных.
  
 
== manipulation ==
 
== manipulation ==

Revision as of 22:11, 9 May 2019

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

Переменные - это символьные последовательности, которые определяет программист и для которых выделяется память (integer, char и т.д.). Они могут находится в области видимости всей программы (глобальные переменные) или только внутри процедуры, функции, метода (локальные переменные).

Объявление

Переменные объявляются в секции var. В Паскале каждая переменная имеет тип данных, известный уже на этапе времени компиляции (и пусть это будет тип данных variant). Переменная объявляется парой [math]\displaystyle{ (\text{identifier}, \text{data type identifier}) }[/math], разделенный двоеточием:

var
	foo: char;

В соответствии с требованиями к пространству типа данных соответствующий объем памяти резервируется в стеке, как только вводится соответствующая область видимости переменной. В зависимости от того, где находится var-секция, вы можете говорить о глобальных или локальных переменных.

manipulation

Variables are manipulated by the assignment operator :=. Furthermore a series of built-in procedures implicitly assign values to a variable:

definition

A variable can be defined, that means declared and initialized, in one term by doing the following.

var
	x: integer = 42;

Note, this syntax is not original Pascal. In Pascal declarations and assignments are kept apart by design. This syntax kind of breaks that principle.

access

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).

memory alias

In conjunction with 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.

see also