Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Pointer to ARRAY in Delphi

  1. #11
    Weps
    Guest

    Pointer to ARRAY in Delphi

    Maybe your definition of unbounded is different from what I've learned, please enlighten me.

  2. #12

    Pointer to ARRAY in Delphi

    Simply take a look at ?ëu?±o's request, you have a pointer to an array with (at compile time) an unknown amount of array elements in memory. You cannot use a dynamic array, since a dynamic array tries to allocate an array dynamically on the heap.

  3. #13
    Weps
    Guest

    Pointer to ARRAY in Delphi

    I don't know where you got your information but it is wrong.

    There are two types of arrays, one where at compile time is known how many elements are in it (bounded) and one where you do not (unbounded).

    In Delphi (Pascal) the second one is also referred to as a dynamic array, dynamic in the sense that the bounds (number of elements) can change at runtime.

    In delphi, bounded arrays are declared as "array[a..b] of" where a <= b
    and unbounded (dynamic) array as "array of".

    The problem that the OP has, is that the declaration for unbounded arrays is different in c and Delphi, thus an unbounded array in c (simply a pointer) does not match an unbounded array in delphi (an actual TYPE).

    [pascal]
    type
    PElement = ^TElement;
    TElement = record
    ...
    End;

    procedure DoSomething;
    var
    FirstElement: PElement;
    begin
    // fetch array
    FirstElement := get_list;

    // loop
    For K := 1 to NumberOfElementsInArray do Begin
    ...
    Inc( FirstElement);
    End;
    End;
    [/pascal]

    The obvious thing here is that "get_list" doesn't tell you how many elements are in the list, which can cause/cause a lot of bugs. (That's why they made a "dynamic array" type in Delphi....)

  4. #14

    Pointer to ARRAY in Delphi

    And you don't understand that the count for the items is easily returned through an out parameter to get_list. Either way is perfectly valid. This isn't Python with "only one way to write things," this is Pascal.

  5. #15
    Weps
    Guest

    Pointer to ARRAY in Delphi

    Quote Originally Posted by Robert Kosek
    And you don't understand that the count for the items is easily returned through an out parameter to get_list. Either way is perfectly valid. This isn't Python with "only one way to write things," this is Pascal.
    Did I write "I don't understand how to get the count for the items?"

  6. #16

    Pointer to ARRAY in Delphi

    No, you were too busy digging yourself a pit. You aren't very helpful when you come out with a "you don't know anything" attack like you did.

    Delphi does not, most emphatically not, support unbounded arrays! If you use a dynamic array you aren't "unbounded" but rather moving the bounds each and every time you resize the array. Believe me, just try doing a few million resizes in both directions and timing it against mere array modification (same number of items). It is not unbounded. Dynamically bounded and very useful, but not unbounded. While what you give does work so does Nuno's.

  7. #17
    Weps
    Guest

    Pointer to ARRAY in Delphi

    I'm not attacking anybody, I merely get a bit ticked off when I see untrue statements. And I get a bit upset when I see people telling others to just go mess with compiler settings to achieve a result. Now that's helpful.

    Anyways, just show me the definition of "unbounded arrays", in english. I already showed you what I think it is.

  8. #18

    Pointer to ARRAY in Delphi

    Quote Originally Posted by Weps
    In Delphi (Pascal) the second one is also referred to as a dynamic array, dynamic in the sense that the bounds (number of elements) can change at runtime.
    Exactly. It can change at runtime, but there is an upper bound. For an unbounded array, no information exists about its bounds, not at compile time not at runtime.

  9. #19

    re-

    Mister Weps, yes it is true that dynamic arrays are used for using array with variable sizes at run time, however when you define the size using length(array,numelements) then Delphi (or what ever pascal compiler) will allocate that numelements size memory and will store a pointer and reference count; but the original problem is about how accesing elements into a block memory already allocated inside a C dll.

    So you have a DLL which allocate internally an array and it returns a pointer to the first element, how you access in delphi easily more elements?, you cant just define a dinamic array in the delphi side, set the same length as the dll array and overwrite it with the returned pointer, (you will have a leek!);

    so actually, after analizing the problem i think that

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

    tp.

    Ps.
    Indee i dont see neither in the original code where it is returned the amount elements allocated by the DLL, probably in a different call.

Page 2 of 2 FirstFirst 12

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
  •