[fpc-pascal] Questions regarding arrays

Graeme Geldenhuys graemeg.lists at gmail.com
Wed Sep 22 11:25:29 CEST 2010


Hi,

I'm have some tough times with arrays (I use them very little). With
trial and error I found that to use a pointer to an array of int32
values returned by a C API (Xlib to be exact), I have to define it as
follows in Object Pascal.

type
  TAtomArray = array[0..0] of TAtom;
  PAtomArray = ^TAtomArray;

var
  xdndtypes: PAtomArray;
  a: TAtom;  // just a culong type
begin
   ...
    XGetWindowProperty(Display, FSrcWinHandle,
        XdndTypeList, 0, 16000,
        TBool(False),
        AnyPropertyType,
        @actualtype, @actualformat, @count, @remaining,
        @xdndtypes);
    // 'count' tells me the amount of items in the returned xdndtypes array
    // so I can access the array items as follows
    a := xdndtypes^[i]
   ...
   XFree(xdndtypes);
end;


Now if I change TAtomArray to the follow, then my code doesn't work. :-)

  TAtomArray = array of TAtom;   // a dynamic array


So what exactly is the difference between these two?

    TAtomArray = array[0..0] of TAtom;
 vs
    TAtomArray = array of TAtom;


Is array[0..0] not actually a dynamic array, but just a static array
with a single item? If so, then why does the returned value from the C
API call, which returns a pointer to an array of culong's (normally
more that one item) work? Somewhere I'm getting confused with all
this, but would love to understand it correctly, and why it works. :)


>From the XGetWindowProperty() man page, it says the the following
about the return type which get stored in xdndtypes value (the last
parameter in the declaration below).

       int XGetWindowProperty(Display *display, Window w, Atom
property, long long_offset, long
              long_length, Bool delete, Atom req_type, Atom
*actual_type_return, int *actual_for‐
              mat_return, unsigned long *nitems_return, unsigned long
*bytes_after_return,
              unsigned char **prop_return);

"If the returned format is 32, the property data will be stored as an
array of longs (which in a 64-bit application will be 64-bit values
that are padded in the upper 4 bytes)."


-- 
Regards,
  - Graeme -


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



More information about the fpc-pascal mailing list