PDA

View Full Version : Dynamic array starting at 1



marmin
21-11-2006, 03:25 PM
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.

NecroDOME
21-11-2006, 03:43 PM
Something like this?
MyArray: array [1..99] of Integer;
EDIT - oops it static :S sorry :S .... pfff
MyArray: array of Integer;

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

marmin
21-11-2006, 03:49 PM
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? :wink:

cronodragon
21-11-2006, 04:02 PM
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;

...

NecroDOME
21-11-2006, 04:04 PM
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.

marmin
21-11-2006, 04:11 PM
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?

cronodragon
21-11-2006, 04:17 PM
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.

technomage
21-11-2006, 07:38 PM
To completely remove references to the index of an array I always use


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

end;
...


I don't have to worry about the indexes then
:D

tpascal
25-11-2006, 04:33 PM
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;

Diaboli
28-12-2006, 02:13 AM
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...

cronodragon
28-12-2006, 02:24 PM
I think 1 based arrays are useful because you can use unsigned integer types to index them, and use 0 to indicate a void index... with 0 based arrays you need signed types, then values lower than -1 are wasted.

WILL
28-12-2006, 03:18 PM
Thats a very good point. However the counter-balance to that is that when using the index in some calculations, you'll often have to do more work to make it work with a 1-based index. And it doesn't generally look as clean or flow well either.