Dynamic array and its features

An array is generally called an ordered set of elements, each of which has a certain (same) type. Arrays are static and dynamic. The length of the first is set at the programming stage, i.e. before starting the program to run, the second - in the course of execution.
For a static array, the description must determine the number of elements that cannot be changed (increase or decrease) during the program operation. When you start a program that uses a static array, a certain number of bytes is allocated in memory for execution to store its elements. This amount of memory will be assigned to the program until it finishes its work. Even if this memory will not be used, no other program code will be able to access it.
The Pascal programming language can only work with static arrays. Therefore, if you want to work with a sequence of variable length, you can describe the structure, for example, of one hundred elements, and use at different stages a different number of elements, not exceeding the number 100. And this, of course, is unreasonable.
Such a problem does not exist in the Delphi IDE . A dynamic array allows not describing the number of elements in the description, but determining it during program execution. A dynamic array can be described in the Var section as follows:
Var Massive: array of integer

delphi dynamic array

Thus, the structure indicated by the Massive identifier is a linear integer sequence of unknown (yet!) Length. To set the size, the program must use the SetLength procedure, for example, SetLength (Massive, 9). The Massive dynamic array will acquire a dimension equal to the number 9. Now it is determined that the sequence contains nine elements of an integer type, numbered from zero. A dynamic array has these features. Delphi has a procedure that frees memory from a set of numbers when the need to store them has disappeared. This is the Finalize procedure, in our case it will be applied as follows: Finalize (Massive).

dynamic array

Similarly, you can describe and apply multidimensional dynamic arrays in Delphi. For example, a two-dimensional dynamic structure will be described as follows:
Var Massive: array of array of integer
If necessary, the columns of the matrix can be of different lengths. This is also specified by the SetLength procedure.

dynamic array delphi

It often happens, especially in large and complex programs, that some data structures are used from time to time or only at the beginning / end of the program. In this case, it would be very wasteful to keep a place in RAM "in reserve". A dynamic array is one of the ways to rationally allocate resources to a computing system. Although it has some disadvantages. Firstly, it is not always convenient numbering of elements from scratch. Secondly, the programmer needs to constantly understand at each point in the program code what state the dynamic array is in. But its merits make all these difficulties ridiculous. In particular, if you want to transfer large amounts of data from a subroutine, then you cannot do without a dynamic way of presentation.

Source: https://habr.com/ru/post/K9271/


All Articles