Delphi 使用SetLength后的猜想验证
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm2 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} type tsubjects = record chinese:string[255]; mathematics:string[255]; english:string[255]; end; ptstudents = ^tstudents; tstudents = record id:Integer; scores: array of tsubjects; end; var pstudents:ptstudents = nil; procedure TForm2.btn1Click(Sender: TObject); begin New(pstudents); SetLength(pstudents.scores, 100000); SetLength(pstudents.scores, 0); end; end.
在开发邮件的过程中,有考虑系统邮件占用大量内存,结构体里使用了动态数组,排查内存泄露,
以上测试程序得知,setlength内部实现了 自动的内存管理申请和释放