【原】DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别

在应用中发现静态数组和动态数组是有区别的:

procedure TForm1.Button1Click(Sender: TObject);
var
  RsltStream: TMemoryStream;
  picBuffer: TByteArray;
begin
  RsltStream := TMemoryStream.Create;
  RsltStream.LoadFromFile('e:\pic.txt');
  RsltStream.Position := 0;                                        // 流读取操作前必须将指针回到起始位
  RsltStream.ReadBuffer(picBuffer,RsltStream.Size);    //静态数组中picBuffer 和picBuffer[0] 的指针是相同的
end;

 

procedure TForm1.Button1Click(Sender: TObject);
var
  RsltStream: TMemoryStream;
  picBuffer: TBytes;   //array of byte
begin
  RsltStream := TMemoryStream.Create;
  RsltStream.LoadFromFile('e:\pic.txt');
  RsltStream.Position := 0;                                           // 流读取操作前必须将指针回到起始位

  setlength(picBuffer, RsltStream.Size);                         //给动态数组定义长度
  RsltStream.ReadBuffer(picBuffer[0],RsltStream.Size);   //动态数组中必须用picBuffer[0] ,否则地址错误,数据也是错误的

end;

你可能感兴趣的:(Delphi)