首页  编辑  

字符串引用计数

Tags: /超级猛料/Language.Object Pascal/指针、内存相关/   Date Created:

获取字符串的引用计数的次数

function GetAnsistringRefcount(const S: string): Cardinal;

asm

 or eax, eax

 jz @done

 sub eax, 8

 mov eax, dword ptr [eax]

@done:

end;

procedure TForm1.Button1Click(Sender: TObject);

var

 S1, S2: string;

begin

 memo1.lines.Add(Format('Refcount at start: %d',

   [GetAnsistringRefcount(S1)]));

 S1 := StringOfChar('A', 10);

 memo1.lines.Add(Format('Refcount after assignment: %d',

   [GetAnsistringRefcount(S1)]));

 S2 := S1;

 memo1.lines.Add(Format('Refcount after S2:=S1: %d',

   [GetAnsistringRefcount(S1)]));

 S2 := S1 + S2;

 memo1.lines.Add(Format('Refcount after S2:=S1+S2: %d',

   [GetAnsistringRefcount(S1)]));

end;