所以,我有以下問題。我有2個PChar變量。我首先分配內存,做一些操作,爲第二個變量分配內存 - 在這一步中,第一個變量包含錯誤值(我在調試時看到它)。下面是代碼:GetMem爲一個PChar變量其他PChar變量的內容變化
procedure TReadThread.Execute;
Var
iRead, temp, i, count : Integer;
header, params : PChar;
begin
try
GetMem(header, 12);
iRead := recv(FSocket, header^, 12, 0);
if (iRead<>12) then
raise Exception.Create('Header recieving problem!');
temp := StrToIntDef(String(copy(header,3,4)),0);
if (temp=0) then
raise Exception.Create('Body receiving problem!');
count := temp*SizeOf(Char);
if (count+12<=16384) then
begin
GetMem(params, count);
iRead := recv(FSocket, params^, count, 0);
if (iRead<>count) then
raise Exception.Create('Cant recieve messsage fully!');
end
else
raise Exception.Create('Bad message size (>16 KB)!');
GetMem(FText, temp*SizeOf(Char)+12);
FText := PChar(String(header) + String(params));
FreeMem(header);
FreeMem(params);
except
on E : Exception do
ShowMessage(E.Message);
end;
end;
上線
iRead := recv(FSocket, params^, count, 0);
當我尋找變量標頭值 - 我看到somethind驚人的 - 不是當我在程序的開頭看到的一樣。我如何解決它?
難道你不想讓PChar成爲PAnsiChar?並用'SizeOf(AnsiChar)'替換'SizeOf(Char)'? – Johan 2011-05-21 20:14:41
和這個改變的目的是什麼? – dizpers 2011-05-21 20:20:29
Ansichar只佔用1個字節,Delphi2010中的Char佔用2個字節。請參閱下面的答案。 – Johan 2011-05-21 20:48:29