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

Thread: Dynamic array starting at 1

  1. #1

    Dynamic array starting at 1

    Hello,
    i've a simple question: is it possible to let the dynamic array begin at 1 in stead of 0. I run into all kinds of unneeded trouble, because I always start my fixed arrays at 1. I know this is maybe a small prob but i'm very strict and it would be splendid to have it.
    Marmin^.Style

  2. #2
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Dynamic array starting at 1

    Something like this?
    [pascal]MyArray: array [1..99] of Integer;[/pascal]
    EDIT - oops it static sorry .... pfff
    MyArray: array of Integer;

    To only think I can say is:
    [pascal]for i := 1 to High(MyArray)+1 do
    MyArray[i-1] := 0;[/pascal]
    NecroSOFT - End of line -

  3. #3

    Dynamic array starting at 1

    Yes, but that's .. not nice.
    Better is:
    array: array of integer;
    array2: array [1..50] of integer;

    setlength (array, 50);
    for x:=1 to 50 do begin
    array[x]:=array2[x]; end;

    (would give error in d.)

    Much more conveniant, right?
    Marmin^.Style

  4. #4

    Dynamic array starting at 1

    You can always use out of range tricks:

    type
    parr = ^tarr;
    tarr = array[1..1] of Byte;

    var
    src: array of Byte;
    mask: parr;

    ...

    SetLength(src, 100);
    mask := @src[0]; // does it work with "mask := src;" ? maybe...

    for i := 1 to Length(src) do
    mask[i] := x;

    ...

  5. #5
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Dynamic array starting at 1

    http://www.delphibasics.co.uk/Article.asp?Name=Arrays

    Note that we have not given the starting index of the array. This is because we cannot - dynamic arrays always start at index 0.
    NecroSOFT - End of line -

  6. #6

    Dynamic array starting at 1

    Quote Originally Posted by cronodragon
    You can always use out of range tricks:

    type
    parr = ^tarr;
    tarr = array[1..1] of Byte;

    var
    src: array of Byte;
    mask: parr;

    ...

    SetLength(src, 100);
    mask := @src[0]; // does it work with "mask := src;" ? maybe...

    for i := 1 to Length(src) do
    mask[i] := x;

    ...
    But I think that will overwrite memory space tthat's used?
    Marmin^.Style

  7. #7

    Dynamic array starting at 1

    Quote Originally Posted by marmin
    Quote Originally Posted by cronodragon
    You can always use out of range tricks:

    type
    parr = ^tarr;
    tarr = array[1..1] of Byte;

    var
    src: array of Byte;
    mask: parr;

    ...

    SetLength(src, 100);
    mask := @src[0]; // does it work with "mask := src;" ? maybe...

    for i := 1 to Length(src) do
    mask[i] := x;

    ...
    But I think that will overwrite memory space tthat's used?
    That's why it is an out of range trick. Just be careful to set the limits 1 to Length(array). And anyways, with dynamic array you have to take the same care, because the compiler is not going to tell you if there is an out of range error if you use a variable for indexing, instead of a constant.

  8. #8

    Dynamic array starting at 1

    To completely remove references to the index of an array I always use

    [pascal]
    var data: array of Integer;
    ...
    ...
    for i := Low(data) to High(data) do
    begin

    end;
    ...
    [/pascal]

    I don't have to worry about the indexes then
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  9. #9

    re-

    I had a similar problem once, my old program was originally coded using static arrays (coded in delphi 3 i think), there was several places where my array is accesed looping from 1 to numelements; then my users requested me to remove the limit and allow them to define the length at room time; so i changed the structure definition to dinamic array but instead of looking every place where the arrays is accesed in the loop i just define the array as setlength(rooms, numelements+1) so all my old for k:=1 to numelements loops still compatibles; just element 0 remain unused, but that is not big deal.

    Since then almost all my code always uses dinamic arrays, and i am acustomed to write my loops as for k:=0 to numelements-1, but somtimes i am missing the flexibility i have to copy static arrays just using somthin like rooms2:=rooms;

  10. #10

    Dynamic array starting at 1

    why noy use 0 as start in your static arrays too? stricktly speaking, isnt that the "right" way to do it?

    iallways start all arrays at 0 cant see any reason not to...

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
  •