Quote Originally Posted by AthenaOfDelphi View Post
On the setLength front, you should (unless I'm mistaken) always remember to setLength to 0 when you're finished with the array.
Dynamic arrays are reference counted, so you don't need to explicitly call SetLength to zero. They will be released automatically when the variable goes out of the scope.

Quote Originally Posted by AthenaOfDelphi View Post
Another option you have though is to use a single array in the first place and use a function to generate an index when you access it using x,y,z. If you use your example dimensions, the function could look like this:-

Code:
function getIndex(x,y,z:integer):integer;
begin
  result:=(z*256*256)+(y*256)+x;
end;
A faster, possibly more efficient variation on this could be:-

Code:
function getIndexFast(x,y,z:integer):integer;
begin
  result:=(z shl 16)+(y shl 8)+x;
end;
As long as optimizations are enabled, both versions will have equivalent performance as the compiler replaces 256 multipliers with bit shifts. You can confirm this by turning optimization on and then watching generated assembly code. The only difference in this case will be the second approach being more difficult to understand, so I'd recommend sticking to the first approach.