动态数组为什么要释放?

---------

Delphi动态数组释放问题_赋值

 

 

数组释放的三种方法:

Delphi属性参数和数组释放 javascript:void(0)

 

--------------

Unit开始

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7   Dialogs, StdCtrls;
  8 
  9 type
 10   TForm1 = class(TForm)
 11     Button1: TButton;
 12     Button2: TButton;
 13     Memo1: TMemo;
 14     Button3: TButton;
 15     Button4: TButton;
 16     procedure Button1Click(Sender: TObject);
 17     procedure Button2Click(Sender: TObject);
 18     procedure Button3Click(Sender: TObject);
 19     procedure Button4Click(Sender: TObject);
 20   private
 21     { Private declarations }
 22   public
 23     { Public declarations }
 24   end;
 25 
 26 var
 27   Form1: TForm1;
 28 
 29 implementation
 30 
 31 var
 32   GlobalArray:array of array of string;
 33 {$R *.dfm}
 34 
 35 procedure TForm1.Button1Click(Sender: TObject);
 36 var
 37   i,j,vCount:Integer;
 38 begin
 39   //初始化GlobalArray
 40   SetLength(GlobalArray,5);
 41   for i:=Low(GlobalArray) to High(GlobalArray) do
 42   begin
 43     SetLength(GlobalArray[i],5);
 44   end;
 45   //为这个GlobalArray赋值
 46   vCount:=0;
 47   Memo1.Lines.Add('------GlobalArray--赋值开始-------Button1-------');
 48   for i:=Low(GlobalArray) to High(GlobalArray)  do
 49   begin
 50     for j:=Low(GlobalArray[i]) to High(GlobalArray[i])  do
 51     begin
 52       Inc(vCount);
 53       GlobalArray[i,j]:='GlobalArray_'+inttostr(vCount);
 54       Memo1.Lines.Add(GlobalArray[i,j]);
 55     end;
 56   end;
 57   Memo1.Lines.Add('------GlobalArray--赋值结束-------Button1-------');
 58 end;
 59 
 60 procedure TForm1.Button2Click(Sender: TObject);
 61 var
 62   i,j,vCount:Integer;
 63 begin
 64   //初始化GlobalArray
 65   SetLength(GlobalArray,3);
 66   for i:=Low(GlobalArray) to High(GlobalArray) do
 67   begin
 68     SetLength(GlobalArray[i],3);
 69   end;
 70 
 71   vCount:=0;
 72   Memo1.Lines.Add('------GlobalArray--不赋值显示 开始------Button2--------');
 73   for i:=Low(GlobalArray) to High(GlobalArray)  do
 74   begin
 75     for j:=Low(GlobalArray[i]) to High(GlobalArray[i])  do
 76     begin
 77       Memo1.Lines.Add(GlobalArray[i,j]);
 78     end;
 79   end;
 80   Memo1.Lines.Add('------GlobalArray--不赋值显示 结束------Button2--------');
 81 
 82   //为这个GlobalArray赋值
 83   vCount:=0;
 84   Memo1.Lines.Add('------GlobalArray--赋值开始------Button2--------');
 85   for i:=Low(GlobalArray) to High(GlobalArray)  do
 86   begin
 87     for j:=Low(GlobalArray[i]) to High(GlobalArray[i])  do
 88     begin
 89       Inc(vCount);
 90       GlobalArray[i,j]:='GlobalArray_Button2_'+inttostr(vCount);
 91       Memo1.Lines.Add(GlobalArray[i,j]);
 92     end;
 93   end;
 94   Memo1.Lines.Add('------GlobalArray--赋值结束------Button2--------');
 95 end;
 96 
 97 procedure TForm1.Button3Click(Sender: TObject);
 98 var
 99   i:Integer;
100 begin
101   for i:=Low(GlobalArray) to High(GlobalArray) do
102   begin
103     GlobalArray[i]:=nil;
104   end;
105   GlobalArray:=nil;
106 end;
107 
108 procedure TForm1.Button4Click(Sender: TObject);
109 var
110   i,j,vCount:Integer;
111 begin
112   //初始化GlobalArray
113   SetLength(GlobalArray,5);
114   for i:=Low(GlobalArray) to High(GlobalArray) do
115   begin
116     SetLength(GlobalArray[i],5);
117   end;
118 
119   vCount:=0;
120   Memo1.Lines.Add('------GlobalArray--不赋值显示 开始------Button4--------');
121   for i:=Low(GlobalArray) to High(GlobalArray)  do
122   begin
123     for j:=Low(GlobalArray[i]) to High(GlobalArray[i])  do
124     begin
125       Memo1.Lines.Add(GlobalArray[i,j]);
126     end;
127   end;
128   Memo1.Lines.Add('------GlobalArray--不赋值显示 结束------Button4--------');
129 
130   //为这个GlobalArray赋值
131   vCount:=0;
132   Memo1.Lines.Add('------GlobalArray--赋值开始------Button4--------');
133   for i:=Low(GlobalArray) to High(GlobalArray)  do
134   begin
135     for j:=Low(GlobalArray[i]) to High(GlobalArray[i])  do
136     begin
137       Inc(vCount);
138       GlobalArray[i,j]:='GlobalArray_Button4_'+inttostr(vCount);
139       Memo1.Lines.Add(GlobalArray[i,j]);
140     end;
141   end;
142   Memo1.Lines.Add('------GlobalArray--赋值结束------Button4--------');
143 end;
144 
145 end.

 

Unit结束

 

--Form开始

 1 object Form1: TForm1
 2   Left = 626
 3   Top = 279
 4   Width = 594
 5   Height = 675
 6   Caption = 'Form1'
 7   Color = clBtnFace
 8   Font.Charset = DEFAULT_CHARSET
 9   Font.Color = clWindowText
10   Font.Height = -11
11   Font.Name = 'MS Sans Serif'
12   Font.Style = []
13   OldCreateOrder = False
14   PixelsPerInch = 96
15   TextHeight = 13
16   object Button1: TButton
17     Left = 288
18     Top = 24
19     Width = 225
20     Height = 25
21     Caption = 'Button1_GlobalArray初始化并赋值'
22     TabOrder = 0
23     OnClick = Button1Click
24   end
25   object Button2: TButton
26     Left = 264
27     Top = 192
28     Width = 281
29     Height = 25
30     Caption = 'Button2_GlobalArray之再使用'
31     TabOrder = 1
32     OnClick = Button2Click
33   end
34   object Memo1: TMemo
35     Left = 0
36     Top = 0
37     Width = 257
38     Height = 637
39     Align = alLeft
40     ImeName = '中文(简体) - 搜狗拼音输入法'
41     ScrollBars = ssBoth
42     TabOrder = 2
43   end
44   object Button3: TButton
45     Left = 312
46     Top = 104
47     Width = 75
48     Height = 25
49     Caption = 'Button3'
50     TabOrder = 3
51     OnClick = Button3Click
52   end
53   object Button4: TButton
54     Left = 280
55     Top = 304
56     Width = 257
57     Height = 25
58     Caption = 'Button4_GlobalArray之再使用'
59     TabOrder = 4
60     OnClick = Button4Click
61   end
62 end

 

--Form结束