Object Pascal dynamic array

xiaoxiao2021-03-06  23

When the dynamic array in Delphi, some confused.

1. Is the dynamic array be a pointer? Dynamic arrays typically exhibit pointer function, first look at the example: procedure dymarrtest (); var A, b: array of integer; begin setlength (a, 3); A [0]: = 0; B: = A; B [0]: = 1; showMessage (INTTOSTR (A [0])); END; in the above program, A, B array points to the same address, so change B [0], result a [0] Will equal to 1. Although the dynamic array has a pointer function, its memory management is the type of survival management, no need to manually release.

2, why do you change the value of the array to change the value of the argument? Although the dynamic array will show the function of the pointer, when it is directly used to declare the parameter, when it changed the shape, it will not change the argument, see the following example: Procedure DymarrParam (Arr: array of integer); VAR i : Integer; Begin for i: = 0 to Length (arr) - 1 do arr [i]: = 2;

Procedure TForm1.Button1Click (Sender: TOBJECT); VAR A: Array of Integer; I: Integer; Begin SetLENGTH (A, 2); for i: = 0 to Length (a) do a [i]: = 1; DymarrParam A); // attempt to change the elements value in the array to 2 caption: = INTOSTR (a [0]) ',' INTOSTR (a [1]); END; can see that it does not have real parameter group The a element value is 2. The reason is that this transfer method compiler has copied a copy of a A in the DymarrParam, rather than changing A value as expected in 1 point, and visible dynamic arrays is not equal to the same pointer.

3, how to change the real-gate value through the metallization of the dynamic array? 1> VAR is clear in Delphi, in the DYMarrParam process, by VAR modification, Procedure DymarrParam (VAR Arr: Array Of Integer); You can use Const modifier when you do not want to change the real gate Participation, clearly indicating that it is not modified, and Procedure DymarrParam (Const ArRay of Integer); if the arr is assigned, it will be compiled without passing. 2> Type another is to redefine a type with TYPE, for example: Type TDYMINTARRAY = Array Of Integer; // In the System unit has declared a TBoundArray, use it to use it to declare, as follows: Procedure DymarrParam Arr: TDYMINTARRAY; the type of arguments used in this function must also be TDYMINTARRAY. In this way, it is also possible to change the argument by a mediate change without VAR modification. In this case, the value of the ginseng value can be modified by modifying the value of the argument.

Reference: 1. Borland (r) Softeware Corporation: Delphi Help 2. Steve Teixeira and Xavier Pacheco: Delphi 6 deverloper's Guide

转载请注明原文地址:https://www.9cbs.com/read-43504.html

New Post(0)