[fpc-pascal] Question about Deleting elements in Dynamic Array

Vincent Snijders vsnijders at vodafonevast.nl
Wed Apr 28 08:37:11 CEST 2010

Bihar Anwar schreef:
> I've tried to use Move() instead of Copy(). Any objection with the following code?

Yes, at first glance without much thinking, I don't think it is safe. Did you think 
through the consequences of copying reference counted types (ansistring in this case 

> var
>   a: array of string;

shortstring of ansistring?

> SetLength(a, 5);
> a[0] := 'aa'; a[1] := 'bb'; a[2] := 'cc'; a[3] := 'dd'; a[4] := 'ee';
> System.Move(a[3], a[0], 2 * SizeOf(string) );    // instead of  a := Copy(a, 3, 2);
> SetLength(a, 2);

Hmm, maybe you are lucky and this does work without crashes, because the contents of 
  the ansistrings are constants. If they were variable strings, then then the move 
would make a[3] and a[0] point to the same string (with reference count 1). The 
setlength would free it, because it frees the memory of the ansistring in a[3].


More information about the fpc-pascal mailing list