高手请赐招!!! (10分)

T

txyx

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个程序:
其中,数据已经读入buffer,iBytesRead是buffer的大小,但是
for i := 0 to iBytesRead-1 do
begin
s:=buffer;//程序编译已经通过,使用中运行在此行停止
appendstr(s,'');
end;
给出错误:Access violation at address 00455c8c in model xxx.exe什么的,
原因不明,请高手赐教!
另外:
PChar(AllocMem(Length)得到的是什么?
因为: AllocMem(Length)的返回值是一个pointer,那么指针的指针指到哪去了???
 
一定是buffer时出界了,仔细检查iBytesRead大小。
 
各个变量什么数据类型你都没有说。
 
这个题目一定是字符串变量了。这样做会被编译程序的错误保护块锁定。
原因是,不管是Char还是String,在读入内存时已经被D的编译器格式化了,
由于您可能定义了s:string,D编译器给你生成了String类型的变量,这样,
你在用二进制的方法读取缓存时,就会引发错误。
另外:你的分配函数好象什么也没有分配啊。
如果指针被分配为Null,那么就是没有指针。
 
delphi提供的例程是:
procedure TForm1.Button1Click(Sender: TObject);

var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
FileClose(iFileHandle);

for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer;
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer));
end;
finally
FreeMem(Buffer);
end;
end;
end;
我认为Buffer := PChar(AllocMem(iFileLength + 1));一句有些问题,
如果他的作用是分配内存的话,好像容量不够吧!
另外StringGrid1.Cells[1,i+1] := Buffer;一句编译也通不过啊
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
667
swish
S
I
回复
0
查看
573
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部