How to Manage a Dynamic Array Inside an Object

Started by Charles Pegge, February 04, 2025, 03:13:19 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

The dynamic array is unique for each object and local to each method, so how is the array content preserved between method calls?

Using remap instead of redim

'REDIM DYNAMIC ARRAY INSIDE AN OBJECT
'
type cc
  '
  def remapaa
    remap int aa(zz,yy,xx)
    @aa=pa
  end def
  '
  sys pa       'pointer to array content
  int zz,yy,xx 'ubounds of array dimensions
  '
  procedure constructor(int z,y,x)
    xx=x : yy=y : zz=z
    pa=getmemory zz*yy*xx*sizeof(int)
    remapaa
  end procedure
  '
  function set(int v,z,y,x)
    remapaa
    aa(z,y,x)=v
  end function
  '
  function get(int z,y,x) as int
    remapaa
    return aa(z,y,x)
  end function
  '
  procedure destructor()
    freememory pa
  end procedure
  '
end type
'
'test
'#recordof cc
new cc c(5,5,5)
c.set(42,4,3,2)
print c.get(4,3,2)
del c