Let's examplify the REDIM exclusion cases

Forum for discussion about the documentation project.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Let's examplify the REDIM exclusion cases

Post by fxm »

Multi-dimensional pointers P[a][c] have no restriction for resizing while preserving elements, at the opposite to multi-dimensional arrays A(a, b, c), but they use a little more memory and the allocation / deallocation is much more complex.

Example for a 2-dimensional pointer (resizing the second dimension while preserving elements):

Code: Select all

Dim As Integer maxIndex(1 To 2) = {2, 3}

Dim As Integer Ptr Ptr P = New Integer Ptr [maxIndex(1)+1]
For I1 As Integer = 0 To maxIndex(1)
  P[I1] = New Integer [maxIndex(2)+1]
Next I1

For I1 As Integer = 0 To maxIndex(1)
  For I2 As Integer = 0 To maxIndex(2)
    P[I1][I2] = I1*10 + I2
  Next I2
Next I1

For I1 As Integer = 0 To maxIndex(1)
  For I2 As Integer = 0 To maxIndex(2)
    Print P[I1][I2],
  Next I2
  Print
Next I1
Print

maxIndex(2) = 4
For I1 As Integer = 0 To maxIndex(1)
  P[I1] = Reallocate(P[I1], (maxIndex(2)+1)*Sizeof(Integer))
Next I1

For I1 As Integer = 0 To maxIndex(1)
  P[I1][maxIndex(2)] = I1*10 + maxIndex(2)
Next I1

For I1 As Integer = 0 To maxIndex(1)
  For I2 As Integer = 0 To maxIndex(2)
    Print P[I1][I2],
  Next I2
  Print
Next I1

For I1 As Integer = 0 To maxIndex(1)
  Delete [] P[I1]
Next I1
Delete [] P

Sleep

Code: Select all

 0             1             2             3
 10            11            12            13
 20            21            22            23

 0             1             2             3             4
 10            11            12            13            14
 20            21            22            23            24
[Added a note about the above code]
These following instructions can be mixed if datatype has no destructor:

Code: Select all

Dim As datatype Ptr p = New datatype [i]
p = Reallocate(p, (i+j)*Sizeof(datatype))
Delete [] p
Last edited by fxm on May 08, 2017 18:23, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Let's examplify the REDIM exclusion cases

Post by Tourist Trap »

fxm wrote:Example for a 2-dimensional pointer (resizing the second dimension while preserving elements):
Very instructive.
There is no such a thing than an array of array anyway in FB?

Code: Select all

dim as integer(5)	array(5)
Maybe it's more an array of ptr that would here be a balanced solution. Something to be tested.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Let's examplify the REDIM exclusion cases

Post by fxm »

An array of pointers works but would induce a weird syntax for accessing an element:

Code: Select all

A(i)[j][k]
instead of
P[i][j][k]
(if we want to keep the possibility of resizing (with preservation) all the dimensions, a one-only-dimensional array must be used)

Therefore, that does not significantly simplify the code (for allocation/deallocation) compared to a solution with only pointers:

Code: Select all

Dim As Integer Ptr Ptr Ptr P = New Integer Ptr Ptr [maxIndex1+1]
or
Redim As Integer Ptr Ptr A(maxindex1)

Delete [] P
or
Erase A
Post Reply