[fpc-pascal] Question about Deleting elements in Dynamic Array
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
> a: array of string;
shortstring of ansistring?
> SetLength(a, 5);
> a := 'aa'; a := 'bb'; a := 'cc'; a := 'dd'; a := 'ee';
> System.Move(a, a, 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 and a point to the same string (with reference count 1). The
setlength would free it, because it frees the memory of the ansistring in a.
More information about the fpc-pascal