Dynamic variable - a variable in the program , a place in the RAM for which is allocated during program execution. In fact, it is a piece of memory allocated by the system to a program for specific purposes while the program is running. In this way, it differs from the global static variable - the portion of memory allocated by the system to the program for specific purposes before starting the program. A dynamic variable is one of the variable's memory classes .
Since a dynamic variable is created during program execution, in many programming languages ββit does not have its own identifier . Work with a dynamic variable is carried out indirectly, through a pointer . Creating such a variable consists in allocating a piece of memory using a special function. This function returns the address in memory that is assigned to the pointer. The process of accessing memory through a pointer is called dereferencing . After working with a dynamic variable, the memory allocated for it must be freed - there is also a special function for this.
An example of creating a dynamic variable using a pointer in Pascal:
type tMyArray = array [1..3] of real;
var pMyArray = ^ tMyArray;
begin
new (pMyArray); {memory allocation for an array of three numbers}
pMyArray ^ [1]: = 1.23456;
pMyArray ^ [2]: = 2.71828;
pMyArray ^ [3]: = 3.14159;
......
In relatively low-level programming languages, pointers are used explicitly, which can lead to serious errors. In higher-level languages, dynamic data types can be styled as dynamic arrays.
An example of creating a dynamic array in Delphi:
var MyArray = array of real;
begin
SetLength (MyArray, 3); {memory allocation for an array of three numbers}
MyArray [0]: = 1.23456; {in Delphi, dynamic arrays are numbered in a C-style: from 0 to n-1}
MyArray [1]: = 2.71828;
MyArray [2]: = 3.14159;
......
In high-level languages, dynamic data types can be styled in other ways β as classes , and the processes of allocating and freeing memory are described in the constructor and destructor of each class.