ComputersProgramming

Dynamic array and its features

An array is generally called an ordered set of elements, each of which has a certain (same type) type. Arrays are static and dynamic. The length of the first is specified in the programming phase, i.e. Before starting the program to run, the second - during the execution.
For a static array, the description should specify the number of elements that can not be changed (increased or decreased) during the program operation. When you run a program that uses a static array, a number of bytes are allocated to the RAM memory to store its elements. This amount of memory will be assigned to the program until it finishes its work. Even if this memory is not 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 variable-length sequence, you can describe a structure, for example, from a hundred elements, and use different numbers of elements at different stages, not exceeding 100. And this, of course, is unreasonable.
Such a problem does not exist in the Delphi IDE . Dynamic array allows you to specify not to specify the number of elements in the description, but to determine it during the execution of the program. A dynamic array can be described in the section Var as follows:
Var Massive: array of integer

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

Similarly, you can describe and apply multi-dimensional 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 matrix columns can be of different lengths. This is also specified by the SetLength procedure.

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. At the same time, it would be very wasteful to keep a place in the RAM "in reserve". A dynamic array is one of the ways to rationally distribute the resources of a computing system. Although it has some drawbacks. First, it is not always convenient to number the elements from scratch. Secondly, the programmer must constantly understand at each point of the program code, in what state is the dynamic array. But his virtues make all these difficulties ridiculous. In particular, if you want to transfer large data from a subroutine, you can not do without a dynamic representation.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.