Forum > General

getting sizes of multidimensional arrays

(1/1)

acp693:
Hello, I was fiddling around with lazarus trying to work out how to obtain the sizes of multidimensional arrays,

I find it is a little counterintuitive, but the following works for a four dimensional array:

--- Code: ---myarray: array of array of array of array of integer;
implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
setlength(myarray,3,10,5,2);
//first dimension
label1.caption := inttostr(high(myarray));
//second
label2.caption := inttostr(high(myarray[0]));
//third
label3.caption := inttostr(high(myarray[0,0]));
//forth
label4.caption := inttostr(high(myarray[0,0,0]));
end;
--- End code ---

Hope it's useful for someone

regards Albert

Zaaphod:
I found this very useful, thank you!

MarkMLl:
There are further problems though: there's no obvious way to ask a (dynamic) array how many dimensions it's got, before asking how many elements there are in each. See

https://lists.freepascal.org/pipermail/fpc-pascal/2017-May/050979.html

for a "fun example" which I raise every five years or so to irritate @PascalDragon :-)

The joke is, of course, that while no True Pascal Programmer likes APL (on which the problem I'm demonstrating is based), Wirth was actually the academic supervisor of the first APL implementation while he was at Stanford.

MarkMLl

BobDog:

For Dynamic arrays:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- generic function GetSize<T,DT>(AnArray: T):int32;varn,size,d:int32;b:Tboundarray;beginsize:=1;d:=DynArrayDim(typeinfo(AnArray));b:=DynArrayBounds(pointer(AnArray),typeinfo(AnArray));writeln('Number of dimensions: ',d);write('size of each dimension ');for n:=low(b) to high(b) do write((1+b[n]),' ');writeln;    for n :=1 to d do size:=size*(b[n-1]+1);       size:=size*sizeof(DT);      exit(size); end;     typeaoi = array of array of array of integer;aoc = array of array of char;aoq = array of array of array of array of int64;ArrayOfArrayOfDouble = array of array of double;  varinput_vectors: ArrayOfArrayOfDouble;   a:aoi=nil;   b:aoc=nil;   c:aoq=nil;    begininput_vectors := [[0.0, 0.0],                  [0.0, 1.0],                  [1.0, 0.0],                  [1.0, 1.0]];      setlength (a,7,5,2);   setlength(b,40,30);   setlength(c,4,2,7,2);       writeln(specialize getsize<aoi,integer>(a),' = size in bytes for integer array');      writeln;   writeln(specialize getsize<aoc,char>(b),' = size in bytes for char array');      writeln;   writeln(specialize getsize<aoq,int64>(c),' = size in bytes for int64 array');      writeln;   writeln(specialize getsize<ArrayOfArrayOfDouble,double>(input_vectors ),' = size  in bytes for double array');      writeln;   writeln('Press return to exit . . .');   readln;   end.