Advertisement
Guest User

Untitled

a guest
Jun 1st, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.70 KB | None | 0 0
  1. program Project26;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses SysUtils;
  6.  
  7. var a, b: array of string;
  8.  
  9. begin
  10.   SetLength(a,1);
  11.   a[0] := IntToStr(Random(1)); // making sure I'm not assigning a constant string
  12.   WriteLn('StringRefCount(a[0])=', StringRefCount(a[0])); // Displays "2"
  13.   SetLength(b,1);
  14.   Move(a[0], b[0], SizeOf(Pointer)); // "Move" the memory
  15.   WriteLn('StringRefCount(a[0]) after Move =', StringRefCount(a[0])); // Displays "2" - wrong, because we now have +1 references to the same string
  16.   SetLength(a,0); // Will actually free the string
  17.  
  18.   WriteLn('StringRefCount(b[0]) after SetLength(a,0) =', StringRefCount(b[0])); // Displays "1" - wrong, because that means the string
  19.  
  20.   ReadLn;
  21. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement