匪
匪匪
Unregistered / Unconfirmed
GUEST, unregistred user!
在函数SendBuffer(var Buffer; Count Integer)中,有一个参数Buffer,是无类型的变量,我在程序中用指针操作时,碰到个问题。
procedure TForm1.BitBtn2Click(Sender: TObject);
var
TempMessageHead: pMessageHead;
TempLogin: pLogin;
TempStream: TMemoryStream;
TempMessageBlock: pMessageBlock;
begin
//登录,发送相应的登录消息
New(TempLogin);
New(TempMessageBlock);
FillChar(TempLogin^, Sizeof(TempLogin), 0);
New(TempMessageHead);
FillChar(TempMessageHead^, Sizeof(TempMessageHead), 0);
IdUDPClient.Host := Edit1.Text;
IdUDPClient.Port := StrToInt(Edit2.Text);
IdUDPClient.Active := True;
TempLogin^.Host := IdUDPClient.Host;
TempLogin^.Port := IdUDPClient.Port;
TempLogin^.UserName := Edit3.Text;
TempMessageHead.Flag := tHead;
TempMessageHead.Sender := tQQUser;
TempMessageHead.Receiver := tQQServer;
TempMessageHead.MsgType := tMsgType_Login;
TempMessageHead.ReceiveIP := IdUDPClient.Host;
TempMessageHead.ReceivePort := IntToStr(IdUDPClient.Port);
TempMessageHead.MsgLength := Sizeof(TempMessageHead) + Sizeof(TempLogin);
///////////////
FillChar(TempMessageBlock.Content.Content, 7000, 0);
TempStream := TMemoryStream.Create;
TempStream.WriteBuffer(TempMessageHead^, SizeOf(pMessageHead));
TempStream.Seek(0, soFromEnd);
TempStream.WriteBuffer(TempLogin^, SizeOf(pLogin));
TempStream.Seek(0, soFromBeginning);
[blue] TempStream.ReadBuffer(TempMessageBlock^, Sizeof(pMessageBlock)); [/blue]//此处的TempMessageBlock中的值与上面的不一致。
JLogin(TempMessageBlock);
TempStream.Free;
Dispose(TempMessageHead);
Dispose(TempMessageBlock);
Dispose(TempLogin);
//显示发送请求在窗口中
// DisplayToMemo();
end;
上面的语句中若用TempMessageBlock^作参数,参数没错,可是结构中的数据与上面的两个结构不一致,但是如果是用TempMessageBlock,则程序出错,是失放指针出错,但结构中的内容却是对的。请问加^与不加在这个程序里面究竟有什么区别?
procedure TForm1.BitBtn2Click(Sender: TObject);
var
TempMessageHead: pMessageHead;
TempLogin: pLogin;
TempStream: TMemoryStream;
TempMessageBlock: pMessageBlock;
begin
//登录,发送相应的登录消息
New(TempLogin);
New(TempMessageBlock);
FillChar(TempLogin^, Sizeof(TempLogin), 0);
New(TempMessageHead);
FillChar(TempMessageHead^, Sizeof(TempMessageHead), 0);
IdUDPClient.Host := Edit1.Text;
IdUDPClient.Port := StrToInt(Edit2.Text);
IdUDPClient.Active := True;
TempLogin^.Host := IdUDPClient.Host;
TempLogin^.Port := IdUDPClient.Port;
TempLogin^.UserName := Edit3.Text;
TempMessageHead.Flag := tHead;
TempMessageHead.Sender := tQQUser;
TempMessageHead.Receiver := tQQServer;
TempMessageHead.MsgType := tMsgType_Login;
TempMessageHead.ReceiveIP := IdUDPClient.Host;
TempMessageHead.ReceivePort := IntToStr(IdUDPClient.Port);
TempMessageHead.MsgLength := Sizeof(TempMessageHead) + Sizeof(TempLogin);
///////////////
FillChar(TempMessageBlock.Content.Content, 7000, 0);
TempStream := TMemoryStream.Create;
TempStream.WriteBuffer(TempMessageHead^, SizeOf(pMessageHead));
TempStream.Seek(0, soFromEnd);
TempStream.WriteBuffer(TempLogin^, SizeOf(pLogin));
TempStream.Seek(0, soFromBeginning);
[blue] TempStream.ReadBuffer(TempMessageBlock^, Sizeof(pMessageBlock)); [/blue]//此处的TempMessageBlock中的值与上面的不一致。
JLogin(TempMessageBlock);
TempStream.Free;
Dispose(TempMessageHead);
Dispose(TempMessageBlock);
Dispose(TempLogin);
//显示发送请求在窗口中
// DisplayToMemo();
end;
上面的语句中若用TempMessageBlock^作参数,参数没错,可是结构中的数据与上面的两个结构不一致,但是如果是用TempMessageBlock,则程序出错,是失放指针出错,但结构中的内容却是对的。请问加^与不加在这个程序里面究竟有什么区别?