Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redimensioning nested UDT arrays
#1
How do I redim a nested UDT array?

say I have

array1.array2.var

and I want to redim array2, how?
Reply
#2
you can't . i think this was asked before.


anyways the way around this is to simple reallocate the memorey you want i.e. use a pointer in you udt like

type foo
bar as integer ptr
end type

foo.bar = allocate(len(bar)*100)

then you can access each element with pointer indexing like
foo.bar[1] = 40

then when you want more memorey

foo.bar = REALLOCATE (foo.bar,len(bar)*400) ' i think i got the syntex right there
Reply
#3
What? Thanks for the help ShadowWolf, but why on earth can you not redimension them? Thats just a pain in the ass. Especially as I really dont want to use pointers.
Reply
#4
i think it somthing to do with the udt memorey being allocated staticly or somthing ask victor Smile
Reply
#5
Why don't you want to use pointer arrays? They're alot more flexible. Wink
Reply
#6
Quote:What? Thanks for the help ShadowWolf, but why on earth can you not redimension them? Thats just a pain in the ass. Especially as I really dont want to use pointers.

I'm the one that asked that question before ;-). When you think about it, the answer makes sense, and it's also FB's strength, sure it's a bit different to use...but once you get used to it (which shouldn't be too long from now) you'll never want to do without them ;-)....
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#7
The problem is the cleaning up, FB would have to keep track of the field types at compile-time to automagically deallocate the dynamic field arrays when they get out of scope.

The field coudn't be an array descriptor too, as its dimensions are unknown, so a implicit pointer would be needed (the size of fields must be known to calculate the UDT's sizes), making each array access slower, because the double dereference.

The only difference in using pointers is that you gotta use CALLOCATE() instead of REDIM and you must free the memory allocated yourself. To access an pointer index, simply use [] instead of (). With arrays above 1 dimension, pointer indexing can become more complex though..
Reply
#8
Ok, thanks guys, I'll use pointers then.

Quote:you must free the memory allocated yourself.
How?


Sorry, im just a complete noob when it comes to pointers.
Reply
#9
with deallocate (pointer goes here)
Reply
#10
Ok, so then how would I set up

array1.array2.var?

because you said I must dim array2 as integer pointer, it means I cant dim it as another UDT...

To make it more clear:

[syntax="qbasic"]type buttontype
x as integer
y as integer
xsize as integer
ysize as integer
end type
type windowtype
x as integer
y as integer
xsize as integer
ysize as integer
title as string
numbuttons as integer
button(0) as buttontype
end type

Dim shared win(0) as windowtype[/syntax]

And I want to be able to redim button(), how could I set it up?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)