我在Delphi中有一个很基本的问题,我不能解决它。
我的代码:
注意:在下面的方法中,DataR是局部的,但通常它是局部概念的一个类var.Just。
class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
SetLength(DataR,Length(input));
Move(input,DataR,Length(input));
end;这将进行编译,但是在执行完Move()之后,DataR = nil。
第二次尝试:
class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
SetLength(DataR,Length(input));
DataR := Copy(input,0,Length(input));
end;这不能在all.Error的第三行编译(DataR := Copy(input....) )说“不兼容的类型”。
问题出在哪里?它们都是字节数组!
发布于 2009-07-13 12:44:58
为什么不使用呢?
SetLength(DataR,Length(input));
for i:=Low(input) to High(input) do
DataR[i]:=input[i];顺便说一句:如果你想让数组作为参数传递,你应该将它们声明为一个类型,例如:
type
TMyArray = array of byte;并使用TMyArray作为参数类型。
编辑:我收到了降低值的通知。在我最初的帖子中,它是针对i:=0的,但i:=Low(输入)更安全、更纯粹。
发布于 2009-07-13 13:29:19
尝尝这个
type
TByteDynArray = array of Byte;
function CopyData(const Input:array of Byte):TByteDynArray;
begin
SetLength(Result, Length(Input));
Move(input[0], Result[0], Length(Input));
end;发布于 2009-07-13 12:53:27
尝试:
class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
SetLength(DataR,Length(input));
Move(input[0],DataR,Length(input));
end;https://stackoverflow.com/questions/1119215
复制相似问题