[fpc-pascal] Dynamic arrays, yet another pitfall
Martin Frb
lazarus at mfriebe.de
Mon Feb 17 21:18:55 CET 2014
On 17/02/2014 20:10, Florian Klämpfl wrote:
> Am 16.02.2014 17:16, schrieb Michael Van Canneyt:
>> 2) SetLength enforces unique ref. count.
> I'am against stating this in the docs. It is an implementation detail.
> The proper way to do a deep copy is using copy.
IMHO, it must be documented, even if it is not meant to be used as a way
to make a copy.
Because it has the side effect, that started this thread.
A:=b;
a[1]:=1; // affects b
Setlength(a,length(a)+1));
a[1]:=2; // does NOT affects b
This is behaviour that affect every user app. If it is not documented,
the "a:=b,a[1]:=1;" is no longer a documented use case of arrays. And if
not documented it is not an allowed usage either?
----------
Btw: How smart is Copy()
a:=nil;
Setlength(2);
a:=copy(a,0,length(a));
Since the source is no longer going to be used, it could be re-used
instead of being copied to new memory.
More information about the fpc-pascal
mailing list