[fpc-pascal] Dynamic arrays, yet another pitfall

Lukasz Sokol el.es.cr at gmail.com
Mon Feb 17 10:38:22 CET 2014


On 17/02/14 06:02, Jürgen Hestermann wrote:
> 
> Am 2014-02-16 17:16, schrieb Michael Van Canneyt:
>> It does exactly that, it says:
>> 1) No COW
> 
> As said already: SetLength *is* a write!
> 
> 

I'm not of compiler background - but I think you're confusing things here.

To 'write' usually means : changing DATA element(s). That's it. You have a sheet of paper of
known size and you write on it/erase/change the text. But the size of the array remains the same.

SetLength is /not/ a write in this sense : because you're resizing the paper you're
going to write on (e.g. adding another page or cutting it to size)

As far I understood, SetLength is changing the /description/ of the array (meta-data?) but not the data itself : 
in this sense, it is not a 'write data' operation.

It does 'write' as in: change parameters - of the array DESCRIPTION area, but that area is not ARRAY DATA;
and enlarging/shrinking the memory allocation - but that does not change the content of the already
written-to array elements: it may add some blank ones or remove some written-to ones, but the rest
remains UNCHANGED, i.e. they (the just-added or remaining-after-removal ones) are not being written-to 
by doing SetLength.)

That being said, maybe a mention of what it means to the user of the compiler, could actually be beneficial
to overall understanding... like

"
SetLength:

* Enforces unique refcount (refcount := 1).
* If refcount was more than 1 (i.e. 2 or more pointers to the array existed before calling SetLength) 
then it copies array data to new data area, trims or enlarges copied data accordingly and returns new pointer to the
resized array.
* otherwise (if refcount was 1) just trims/enlarges the array data area accordingly.
"

Hope this helps,
-L.




More information about the fpc-pascal mailing list