Search code examples
arraysdelphimultidimensional-arraydynamic-arrays

Storing multidimensional dynamic array


I have a dynamic array of double with 3 dimensions eg.

customArray : array of array of array of double

In the program I set the length of each dimension separately (not rectangular array) and change it when it is needed.

I wonder if the array is stored in a compact memory portion so to save it in a stream at once like writebuffer(customArray,sizeof(customArray)) and later, load it again to the same dynamic array like readbuffer(customArray, savedSize);

Is this possible ?


Solution

  • This is not actually a multi-dimensional array. This is what is known as a jagged array. The inner most dimension is contiguous but the outer dimensions are arrays of pointers.

    So the elements are not stored contiguously. If you wish to write them to a file in contiguous fashion you will need to arrange that by looping over each dimension.

    In pseudo code that would be:

    for i
      for j
        for k
          write(arr[i,j,k]);
    

    Since the innermost dimension is contiguous this could be written as:

    for i
      for j
        write(arr[i,j]);
    

    A jagged array can have member arrays of differing length but I presume that your array has members all of the same length.