[fpc-pascal] Questions regarding arrays

Michael Van Canneyt michael at freepascal.org
Thu Sep 23 11:50:59 CEST 2010

On Thu, 23 Sep 2010, Graeme Geldenhuys wrote:

> On 23 September 2010 09:46, Michael Van Canneyt wrote:
>>> my FPC Language Reference docs with what you mentioned, or see if
>>> Michael van Canneyt could add that info into the official docs so
>>> others can benefit too.
>> What do you want added ? I haven't seen anything which isn't already
>> mentioned somehow in the docs ?
> I'll re-read the docs covering array and dynamic arrays just to make
> sure. What I think is missing, is the "gotcha" usages of arrays and
> pointers to various array types. eg:

[ Snip: I'll see about what I can add to make it more clear ]

> Like I said, I read the FPC lang ref docs regarding arrays, but the
> above information was not immediately obvious to me. I'll re-read the
> docs now to see if I maybe just missed that information (which could
> be possible too).
> Side note:
> Even if the above is in the docs (maybe worded differently), this
> gives me a reason to complete the DocView annotations support anyway,
> so I can add extra info for when my brain is not working so well. :-)
>>> Out of interest. Do you know if dynamic array elements are in sequence
>>> (storage area in memory) too - like static arrays? So could I do the
>> Yes.
> Perfect, using dynamic arrays will be easier (and cleaner
> implementation) when I need to send an array to the C library.
>>> As for freeing the array that the C API returned to me... I don't do
>>> the freeing, I simply notify the C library when I am done using the
>>> array, and the library does the freeing for me.
>> No. This you cannot do, since the pascal memory manager allocated the array,
>> it must also free it. The C memory manager has no idea about this
>> 'memory'. You must manually do a SetLength(myarray,0);
> Maybe you understood me wrong, or indeed there is something I need to
> free myself. What I was referring to in the quoted text above, is when
> the C API allocated the array, and I simply retrieved a pointer to
> that array. In such cases I believe I don't need to do the freeing, I
> simply tell the C API to do the freeing. In the case of Xlib, that is
> done with the XFree() call.

Take care that in this case you should not use a dynamic array to refer to
the memory allocated by the C API:

Dynamic arrays are finalized at the end of a procedure, and since the Pascal
memory manager has no knowledge of the memory allocated by the C lib, things
will go horribly wrong when the dynamic array goes out of scope.


More information about the fpc-pascal mailing list