[fpc-pascal] A warning when Blockwriting/reading dynamic array pointers

Jonas Maebe jonas.maebe at elis.ugent.be
Sat Apr 2 18:53:01 CEST 2011


On 02 Apr 2011, at 18:19, Jürgen Hestermann wrote:

> >> I would never expect an assignmet to nil changing anything else than just the value of the pointer. And for (all?) other pointers this expectation is valid.
> > That expectation is not valid for any reference counted type (be it a dynamic array, ansistring, unicodestring, COM-style interfaces). That's the whole point of reference counted types: they keep track of how many references still exist to the data, and once that reaches zero the data is freed.
> 
> But I just assign a value to a pointer,

No, you assign a value to a dynamic array. A dynamic array is not a plain pointer, just like a COM-style interface is not a plain pointer. Assigning nil to a COM-style interface does not set its value simply to nil either. There is also no difference between assigning the constant nil to a dynamic array variable and assigning a dynamic array with length 0 to a dynamic array variable (since empty arrays are also represented by a nil pointer, even if they are stored inside a variable).

> > The correct way to do so is to call initialize() on the field.
> 
> It's not mentioned in the documentation you refered to (http://www.freepascal.org/docs-html/ref/refsu15.html#x39-450003.3.1).

The documentation for the initialize routine indeed seems to be missing.

> And I don't need any other routine because fillchar does the job.

Fillchar does the job just like adding inline assembler would do the job. It's a hack.

> The irritation just was, that I expected an assignment to nil doing exactly the same as fillchar(..,#0). And I am still astonished that it does more. What other traps are lurking under the hood? It's hard to program if you have to fight with undocumented "features" instead of concentrating on my own mistakes...


If you don't want the compiler to interfere with anything you do, do not use automated types such as ansistring, unicodestring, dynamic array and COM-style interfaces. The whole point of these types is that the compiler will do lots of stuff behind your back, because without that the reference counting cannot work.


Jonas


More information about the fpc-pascal mailing list