View Full Version : Redim non-dynamic array?
ReneMiner
10-05-2013, 19:52
Is it valid to Redim some none-dynamic array?
Maybe I have a fixed size udt-array that holds my data, numbers and strings...
Now user decides to start all over and delete the data. Can I just Redim the array all at once- even if it was not dimensioned with empty parenthesis? Or do I have to write some loop to fill all with zeros and "" ?
ReneMiner
10-05-2013, 20:16
Why what where issue? I don't want to learn powerbasic nor C - i want to learn thinBasic right now. And I'm just interested to find a fast way to delete the content of some fixed-size UDT-array which contains dynamic strings too.
When does the day come you'll simply answer a question instead of advising me to busy myself with something else?
But I've found an answer, obviously is valid, this runs without Error
Uses "CONSOLE"
Type t_type
X As Long
S As String
End Type
Dim X(199) As t_Type
X(25).X = 112
X(25).S = "Hello"
PrintL Str$(X(25))
PrintL X(25).S
ReDim x(199)
PrintL Str$(X(25))
PrintL X(25).S
WaitKey
Petr Schreiber
10-05-2013, 21:41
Hi Rene,
Arrays of elemental types can be redimensioned without problem:
DIM a(5) AS STRING
REDIM a(10) ' -- This will redimension the array and erase its contents
REDIM PRESERVE a(10) ' -- This will redimension the array and preserve its contents
Arrays of UDT can be redimensioned without problem:
TYPE Vector2D
x AS SINGLE
y AS SINGLE
END TYPE
DIM b(5) AS Vector2D
REDIM b(10) ' -- This will redimension the array and erase its contents
REDIM PRESERVE b(10) ' -- This will redimension the array and preserve its contents
Arrays, which are members of UDT cannot be redimensioned:
TYPE MyType
x AS SINGLE
y AS SINGLE
c(10) AS SINGLE
END TYPE
DIM v AS MyType
REDIM v.c(50) ' -- NOT POSSIBLE
REDIM PRESERVE v.c(50) ' -- NOT POSSIBLE
Why is it impossible in this case? User defined types (not just in ThinBASIC, but also in other languages, where they are called struct or record) have fixed size. Redimensioning the array inside UDT would make it grow or shrink, which is unwanted behavior (size in memory would change).
So much the theory.
Because having the UDTs fixed is annoying, especially for strings, Eros managed (thanks to dark magic and unholy rituals) to make dynamic strings inside UDT working.
What? Dynamic strings? Wouldn't the variable length make the UDT memory footprint grow and shrink too?
Yes and no. Eros internally represents the dynamic string using pointer, so the actual string content is stored in memory outside of UDT. When you work with the dynamic string in UDT variable, Eros performs some magic to make it all appear one compact block. So, in theory, it would be possible to have dynamic arrays inside UDT implemented this way too.
Petr
ReneMiner
11-05-2013, 00:05
OK, thanks for explanation. I'm already aware that some dynamic string in UDT is just a Dword
- that's why this does not work correctly and produces orphans in memory and totally weired results:
Uses "CONSOLE"
Type t_type
A As String
B As Long
End Type
Dim foo(8) As t_Type
foo(1).A = "Hello"
foo(2).A = "World"
foo(3) = foo(1)
foo(4) = foo(2)
foo(3).A = "better"
foo(4).A = "yellow sunshine"
foo(5) = foo(3)
foo(6) = foo(4)
foo(6).A = "do not do this!"
Dim surprise As DWord
For surprise = 1 To 6
PrintL Str$(surprise) +" holds: " + foo(surprise).A
Next
WaitKey
- my point was just, since i did not
Dim foo() as t_type
but
Dim foo(someNumberHere) as t_type
I was not aware that thinBasic would allow this to redim- and I expect weired results if I dim some empty dummy-array and place the content to the real one.
I know a couple of other languages where it is not possible to ReDim an array that was not dimensioned explicitely dynamic - with some empty parens...
- but not so in tB... why wasn't it invented a few years earlier? - I wouldn't have wasted all the time to learn all these other languages!