Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redimensioning nested UDT arrays
#11
Go to the plantasy site, get memProcs

Use it..

Code:
type mytype
  arr2 as uinteger ptr
end type

dim arr1(1000) as mytype

arr1(0).arr2 = mem_alloc(1024) 'allocate 1024 bytes
arr1(1).arr2 = mem_alloc(1024) 'allocate 1024 bytes
arr1(0).arr2[0] = 1000
arr1(0).arr2[1] = 2000
arr1(0).arr2[2] = 3000

arr1(1).arr2[0] = 11000
arr1(1).arr2[1] = 12000
arr1(1).arr2[2] = 13000

print arr1(0).arr2[1]  'prints 2000
print arr1(1).arr2[1]  'prints 12000

mem_cleanup 'free all allocated memory

If you dont want to use memProcs, hange mem_alloc to: callocate()
And mem_cleanup to: deallocate memptr

Code:
type mytype
  arr2 as uinteger ptr
end type

dim arr1(1000) as mytype

arr1(0).arr2 = callocate(1024) 'allocate 1024 bytes
arr1(1).arr2 = callocate(1024) 'allocate 1024 bytes
arr1(0).arr2[0] = 1000
arr1(0).arr2[1] = 2000
arr1(0).arr2[2] = 3000

arr1(1).arr2[0] = 11000
arr1(1).arr2[1] = 12000
arr1(1).arr2[2] = 13000

print arr1(0).arr2[1]  'prints 2000
print arr1(1).arr2[1]+arr1(1).arr2[0]  'prints 23000 (12000+11000)

deallocate arr1(0).arr2 'free all allocated memory
deallocate arr1(1).arr2

To reallocate:
memptr = mem_realloc(memptr, new_size)
Or:
memptr = reallocate(memptr, new_size)


The advantage of using memProcs is, apart from offering more functions, that it keeps track of allocated memory, and you only have to call mem_cleanup to free everything you allocated, as opposed to having to add a new deallocate for each allocate.
Reply
#12
I think you missed the point.

array2 must also be a UDT.
Reply
#13
I didnt miss the point.

arr1(0).arr2[index+varoffs] = value

Where index = the array index memory wise
and varoffs is the relative position of the variable..

Example:
type mytype
data as integer
poo as short
blarg as byte
end type


data = 0
poo = 4
blarg = 6

Total size = 7 bytes

First entry = byte 0
Secnd entry = byte 7
Third = byte 14

to get poo of the second array entry:
print arr1(0).arr2[7+4]


Only way to do it if you want dynamic nested arrays with UDTs.. or you could use peek/poke, i beleive they work with UDTs
Reply
#14
Ok, I get it. thnx
Reply
#15
Simply use myfbarray(index1).myfieldarray[index2].myfield, no offset is needed, using them directly and your app would be incompatible with FB if anything is changed/corrected in next versions.

Code:
type myfield
    f1 as short
    f2 as integer
end type

type mytype
    a as integer
    b as short
    myfieldarray as myfield ptr
end type

const indexes = 1000
const fields = 10

    dim myfbarray(0 to indexes-1)  as mytype
    for i = 0 to indexes-1
          myfbarray(i).myfieldarray = callocate( len( myfield ) * fields )

          myfbarray(i).myfieldarray[fields\2].f2 = i  
    next

      print indexes\2; " ="; myfbarray(indexes\2).myfieldarray[fields\2].f2

Anyway, that's a bad example, if "fields" was known at compile-time you could just use an ordinary static array..
Reply
#16
Thanks Victor, that helped a lot Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)