我想知道如何增加 FOR 循环语句中的值。
这是我的代码。
function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
var i:byte;
begin
for i := 0 to Length(MemorySignature) - 1 do
begin
while(MemorySignature[i] = $FF) do inc(i); //<< ERROR <<
if(memorydata[i + position] <> MemorySignature[i]) then Result:=false;
end;
Result := True;
end;
错误是:E2081 分配给 FOR 循环变量“i”。
我正在尝试将旧代码从 C# 转换为 Delphi,但我无法增加“i”。
增加“i”并不是唯一的方法,但我想知道问题出在哪里。
当然,其他人(通常)是正确的。没有说的是,你的循环中的“i”doesn't存在。 Delphi 使用 CPU 寄存器。这就是为什么你不能改变它,这就是为什么你应该使用“for”循环(而不是“while”),因为“for”要快得多。这是修改后的代码(未经测试,但我认为你明白了) - 恕我直言,你也有一些错误 - 也修复了它们:
function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
var i:byte;
begin
Result := True; //moved at top. Your function always returned 'True'. This is what you wanted?
for i := 0 to Length(MemorySignature) - 1 do //are you sure??? Perhaps you want High(MemorySignature) here...
begin
if MemorySignature[i] <> $FF then //speedup - '<>' evaluates faster than '='
begin
Result:=memorydata[i + position] <> MemorySignature[i]; //speedup.
if not Result then
Break; //added this! - speedup. We already know the result. So, no need to scan till end.
end;
end;
end;
...MemorySignature 也应该有一个“const”或“var”。否则,就像现在一样,数组将被复制。这意味着每次调用“检查”时都会变慢。有了“var”,代码不变的情况下事情会快得多,因为 AFAIS MemorySignature 没有改变。
HTH
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)