在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
FillMemory、ZeroMemory 一目了然的两个函数, 但其实它们都是调用了 FillChar; function GetMemBytes(var X; size: Integer): string; var pb: PByte; i: Integer; begin pb := PByte(X); for i := 0 to size - 1 do begin Result := Result + IntToHex(pb^, 2) + #32; Inc(pb); end; end; {GetMemBytes end} //测试: var p1: PAnsiChar; p2: PWideChar; s1: AnsiString; s2: UnicodeString; begin p1 := 'ABCD'; p2 := 'ABCD'; s1 := 'ABCD'; s2 := 'ABCD'; ShowMessage(GetMemBytes(p1,4)); {41 42 43 44} ShowMessage(GetMemBytes(p2,8)); {41 00 42 00 43 00 44 00} ShowMessage(GetMemBytes(s1,4)); {41 42 43 44} ShowMessage(GetMemBytes(s2,8)); {41 00 42 00 43 00 44 00} end;
const num = 10; var p: PChar; begin p := StrAlloc(num); ShowMessage(GetMemBytes(p, num)); {从结果看出 StrAlloc 没有初始化内存} FillMemory(p, num, Byte('A')); ShowMessage(GetMemBytes(p, num)); {41 41 41 41 41 41 41 41 41 41} ZeroMemory(p, num); ShowMessage(GetMemBytes(p, num)); {00 00 00 00 00 00 00 00 00 00} FillChar(p^, num, 'B'); ShowMessage(GetMemBytes(p, num)); {42 42 42 42 42 42 42 42 42 42} StrDispose(p); end;
{测试1} var p: Pointer; begin p := GetMemory(3); ShowMessage(GetMemBytes(p, 3)); ReallocMem(p, 10); ShowMessage(GetMemBytes(p, 10)); {没有初始化} FreeMemory(p); end; {测试2} var p: Pointer; begin p := AllocMem(3); ShowMessage(GetMemBytes(p, 3)); ReallocMem(p, 10); ShowMessage(GetMemBytes(p, 10)); {没有初始化} FreeMemory(p); end;
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论