Search code examples
delphidelphi-xe8

Using GetMem for Allocation of Multidimensional arrays


How can I to create multidimensional(2D, 3D, 4D) arrays using GetMem and PointerMath?


Solution

  • This is better without an extra variable.

    var
      I, J: Integer;
      A: PInteger;
    begin
      GetMem(A, 10 * 10 * SizeOf(Integer));
    
      for I := 0 to 10 - 1 do
      for J := 0 to 10 - 1 do
      PInteger(@A[I * 10])[J] := Random(10);
    
    
      for I := 0 to 10 - 1 do
      for J := 0 to 10 - 1 do
      WriteLn(I,',',J,':',PInteger(@A[I * 10])[J]);