魔鬼代码?
procedure UnpackPlate(sourceFile: string; image:p);
var
k,j,i:integer;
height,width :integer;
PTmp:p;
readbyte:byte;
writebyte:byte;
shiftbyte:byte;
sourceStream :TFileStream;
begin
width := 112; //解压后的宽度
height := 20; //解压后的高度
PTmp := image; //临时指针
if not FileExists(sourceFile) then exit;
try
sourceStream := TFileStream.Create(sourceFile,fmOpenRead or fmShareDenyNone);
try
for i := 0 to height-1 do
begin
sourceStream.Position := (height-1-i)*14;
for j := 0 to (width div 8)-1 do
begin
sourceStream.Read(readbyte,1);
shiftbyte := 1;
for k := 0 to 7 do // k 值从8开始??????
begin
if (shiftbyte and readbyte) > 0 then
begin
writebyte := 0;
end
else
begin
writebyte := 215;
end;
PTmp^:= writebyte;
inc(PTmp);
shiftbyte := shiftbyte shl 1; //一位8次就会报“边界溢出”错
end; //end of for k
end; //end of for j
end; // end of for i
finally
sourceStream.Free;
end;
except
exit;
end;
end;
这是一个过程,其中image是一块内存的指针,在过程外面已经分配好了。这段代码
在以前运行都是好的,昨天发现问题了。
首先,K值竟然是从8开始,共循环8次,到1时就退出K循环了。
其二,shiftbyte := shiftbyte shl 1; 这个左移操作在第8次的时候会抛出一个异常:
---------------------------
Debugger Exception Notification
---------------------------
Project demo.exe raised exception class ERangeError with message 'Range check error'.
其三,还有跟绝的,如果我把左移操作该成:
if k < 7 then
begin
shiftbyte := shiftbyte shl 1;
end;
这个时候再跟踪K值,竟然发现他又是从0开始的了。
其四,i,k值都可以在单步调试的时候可以查看它们的值,而查看j值确报告:由于优化,不能查看
其值????,怎么回事?如果我一定要知到j值呢?
以上四个问题,请大家讨论!
我用的2000 server,d7
昨天还重装过d7,在别的机器上测试这段代码也有同样的问题。