Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Pointer to ARRAY in Delphi

  1. #1

    Pointer to ARRAY in Delphi

    System: Windows
    Compiler/IDE: Lazarus & Delphi (trying multiplatform)
    libraries: RTL

    I'm using a DLL written in C. This library has several functions that use arrays. C uses pointers in most cases so I did something like this:[pascal]PROGRAM example;
    TYPE
    TLISTptr = ^TLIST;
    TLIST = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList[0]);
    END.[/pascal]That compiles and runs perfect using Free Pascal but some users tells me that Delphi can't compile it. So I changed it by this:[pascal]PROGRAM example;
    TYPE
    TLIST_ITEMptr = ^TLIST_ITEM;
    TLIST_ITEM = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    TLISTptr = ^TLIST;
    TLIST = ARRAY OF TLIST_ITEM;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList^[0]);
    END.[/pascal]Free Pascal compiles it but it returns a runtime 216 error. No idea about Delphi.

    By the way this code seems to work both Delphi and FPC:[pascal]PROGRAM example;
    TYPE
    TLIST_ITEMptr = ^TLIST_ITEM;
    TLIST_ITEM = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    TLISTptr = ^TLIST;
    TLIST = ARRAY [0..80] OF TLIST_ITEM;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList^[0]);
    END.[/pascal]But the returned list size may vary from 1 to infinite, so this isn't the solution

    ¬øAlny idea about how to work with a non-sized array pointer in Delphi?

    Thanks
    No signature provided yet.

  2. #2

    Pointer to ARRAY in Delphi

    I've found the answer from another forum.

    [pascal]PROGRAM example;
    TYPE
    TLIST_ITEMptr = ^TLIST_ITEM;
    TLIST_ITEM = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    TLISTptr = ^TLIST;
    TLIST = ARRAY [0..0] OF TLIST_ITEM;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList^[0]);
    END.[/pascal]Note the use of [ 0..0 ] to prevent 'unfair use'.

    It was sooooooo obvious... I'm embarassed... ops:
    No signature provided yet.

  3. #3

    Pointer to ARRAY in Delphi

    @array is NOT SAME as @array[0] or @array[0, 0], it is simply not, just like string[0] is something else, for arrays you need to point your pointer AT THE DATA, NOT at the variable itself, the structure of the variable is not guaranteed to be same on all compilers, but the DATA is, so point at the data and not at the variable, because variable may contain internal structures to reprisent and hold the actual data.

    If you have a dynamic 2d array of integers the data is not even guaranteed to be contigous, there may be internal data between each row of these arrays, not delphi not fpc or any other pascal compiler will guarantee that @array would ever point to data itself and the contigoucy of dynamic array internal structures.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  4. #4

    Re: Pointer to ARRAY in Delphi

    Quote Originally Posted by ?ëu?±o Mart??nez
    By the way this code seems to work both Delphi and FPC:[pascal]PROGRAM example;
    TYPE
    TLIST_ITEMptr = ^TLIST_ITEM;
    TLIST_ITEM = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    TLISTptr = ^TLIST;
    TLIST = ARRAY [0..80] OF TLIST_ITEM;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList^[0]);
    END.[/pascal]But the returned list size may vary from 1 to infinite, so this isn't the solution

    ¬øAlny idea about how to work with a non-sized array pointer in Delphi?

    Thanks
    Wait, first you say "returned list size" but then you are asking to work with non-sized? How do you find out size of this list?

    Even this should work and reserve same space as 0..0 when using it through pointers, but this allows compiler to access up to item[255] manually, where 0..0 only allows for [0] or through variable because of range checking:
    TLIST = ARRAY [0..255] OF TLIST_ITEM;

  5. #5

    Pointer to ARRAY in Delphi

    0..0 (with range checks disabled!!!) is the best solution. Delphi has no facilities for unbounded arrays.

  6. #6

    Pointer to ARRAY in Delphi

    ry to declare TLISTptr as a pointer to array of TLIST items.

    For example: (not tested)

    Code:
     
    PROGRAM example;
    TYPE
    TLIST = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;
    TLISTARRAY = array[0..$FFFF] of TLIST;
    TLISTptr = ^TLISTARRAY;
    
    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';
    
    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    WriteLn (TheList[0]);
    END.
    Last edited by Jimmy Valavanis; 30-03-2012 at 03:50 PM.

  7. #7

    Pointer to ARRAY in Delphi

    Quote Originally Posted by Jimmy Valavanis
    Try to declare TLISTptr as a pointer to array of TLIST items.

    For example: (not tested)
    If that's going to work then you need to dereference the list pointer before indexation: ...^[0]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    Re: Pointer to ARRAY in Delphi

    Quote Originally Posted by User137
    Wait, first you say "returned list size" but then you are asking to work with non-sized? How do you find out size of this list?
    Well, I think the problem is that I'm not a native English speaker. I wanted to say "returned list with size"? not sure.

    Anyway thanks for the comments.

    I would use:[pascal]PROGRAM example;
    TYPE
    TLIST_ITEMptr = ^TLIST_ITEM;
    TLIST_ITEM = RECORD
    Field: INTEGER;
    Other: INTEGER;
    END;

    TLISTptr = ^TLIST;
    TLIST = ARRAY [0..0] OF TLIST_ITEM;

    FUNCTION get_list: TLISTptr; CDECL; EXTERNAL 'lib.dll';

    VAR
    TheList: TLISTptr;
    BEGIN
    { Somewhere }
    TheList := get_list;
    {$R-} { Don't check range. }
    WriteLn (TheList^[0]); { Deference the pointer. }
    {$R+}
    END.[/pascal]Is that correct?
    No signature provided yet.

  9. #9
    Weps
    Guest

    Pointer to ARRAY in Delphi

    Quote Originally Posted by dmantione
    0..0 (with range checks disabled!!!) is the best solution. Delphi has no facilities for unbounded arrays.

    [pascal]Type
    TListItem = record
    a,b,c: integer;
    End;
    TListArray = array of TListItem;[/pascal]


    Doh!

  10. #10

    Pointer to ARRAY in Delphi

    That is not an unbounded array, but a dynamic array. Stored on the heap, therefore not usefull in many circumstances.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •