[fpc-pascal] Questions regarding arrays

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Sep 23 10:51:09 CEST 2010


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:

  * Pointer to first array element, using static array can be:
@myarray or @myarray[0]
     (if your low bound array elements start at 0)

  * Pointer to first array element, using dynamic array can be @mydynarray[0]
     (if your low bound array elements start at 0)
     ... *but* it cannot be:  @mydynarray

   * A pointer to a string (a string is actually a byte array) should
be:  @mystring[1]
     ... and *not* the following:  @mystring   or  @mystring[0]

   * array elements (in static or dynamic arrays) are in sequence in
memory. Handy to
     know if your code interacts with other languages like C.


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.

Now during the opposite action, when I send an array to the C API,
then yes I allocated the memory, so I need to free that memory when I
have no need for the array any more. This is clear to me. I always
develop with heaptrc active, so I can immediately stop when I
allocated memory, but didn't free it (in the case of a external
library doing the allocation one has to be more careful - I know).



-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net:8080/fpgui/



More information about the fpc-pascal mailing list