Recent

Author Topic: getting sizes of multidimensional arrays  (Read 4286 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
getting sizes of multidimensional arrays
« on: June 30, 2009, 03:03:27 pm »
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: [Select]
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;                                                 

Hope it's useful for someone

regards Albert

Zaaphod

  • Newbie
  • Posts: 2
Re: getting sizes of multidimensional arrays
« Reply #1 on: August 27, 2022, 07:14:45 pm »
I found this very useful, thank you!

MarkMLl

  • Hero Member
  • *****
  • Posts: 5595
Re: getting sizes of multidimensional arrays
« Reply #2 on: August 27, 2022, 10:48:13 pm »
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BobDog

  • Sr. Member
  • ****
  • Posts: 365
Re: getting sizes of multidimensional arrays
« Reply #3 on: August 28, 2022, 02:13:56 am »

For Dynamic arrays:
Code: Pascal  [Select][+][-]
  1.  
  2. generic function GetSize<T,DT>(AnArray: T):int32;
  3. var
  4. n,size,d:int32;
  5. b:Tboundarray;
  6. begin
  7. size:=1;
  8. d:=DynArrayDim(typeinfo(AnArray));
  9. b:=DynArrayBounds(pointer(AnArray),typeinfo(AnArray));
  10. writeln('Number of dimensions: ',d);
  11. write('size of each dimension ');
  12. for n:=low(b) to high(b) do write((1+b[n]),' ');
  13. writeln;
  14.     for n :=1 to d do size:=size*(b[n-1]+1);
  15.        size:=size*sizeof(DT);
  16.      exit(size);
  17.  end;
  18.  
  19.  
  20.  
  21.  type
  22. aoi = array of array of array of integer;
  23. aoc = array of array of char;
  24. aoq = array of array of array of array of int64;
  25. ArrayOfArrayOfDouble = array of array of double;
  26.  
  27.  var
  28. input_vectors: ArrayOfArrayOfDouble;
  29.    a:aoi=nil;
  30.    b:aoc=nil;
  31.    c:aoq=nil;
  32.  
  33.    begin
  34. input_vectors := [[0.0, 0.0],
  35.                   [0.0, 1.0],
  36.                   [1.0, 0.0],
  37.                   [1.0, 1.0]];  
  38.  
  39.    setlength (a,7,5,2);
  40.    setlength(b,40,30);
  41.    setlength(c,4,2,7,2);
  42.  
  43.    
  44.    writeln(specialize getsize<aoi,integer>(a),' = size in bytes for integer array');
  45.       writeln;
  46.    writeln(specialize getsize<aoc,char>(b),' = size in bytes for char array');
  47.       writeln;
  48.    writeln(specialize getsize<aoq,int64>(c),' = size in bytes for int64 array');
  49.       writeln;
  50.    writeln(specialize getsize<ArrayOfArrayOfDouble,double>(input_vectors ),' = size  in bytes for double array');
  51.       writeln;
  52.    writeln('Press return to exit . . .');
  53.    readln;
  54.    end.
  55.  
  56.  
  57.  
« Last Edit: September 09, 2022, 10:28:44 pm by BobDog »

 

TinyPortal © 2005-2018