Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic Arrays in User Defined Types
#1
Hi All,

Ok let's say I have the following:

Code:
TYPE SomeType
     Description AS STRING
     Age         AS LONG
END TYPE

TYPE DynamicStructure
     SomeNumber  AS LONG
     SomeName    AS STRING
     SomeArray() AS SomeType
END TYPE

My Quesions are:

1. Can I have a String without a fixed lenght in a User Defined Type like what I'm doing (Knowing that this would be written to a binary file hence no specific structure or length should be needed).

2. Can I have dynamic arrays in User Defined Type definitions like I have here (who's length (ubound) would change elsewhere in the program)?

3. Could I have dymanic arrays of a type that would also have a dynamic array in it's definition? If so, how many levels deep would be allowed?

Thanks all.
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
#2
Dynamic strings are allowed, but dynamic arrays aren't. The easiest work around is to use a pointer array. This is how C/C++ does it.


Code:
TYPE SomeType
     Description AS STRING
     Age         AS LONG
END TYPE

TYPE DynamicStructure
     SomeNumber  AS LONG
     SomeName    AS STRING
     SomeArray AS SomeType Ptr
END TYPE

Sub Main
Dim test As DynamicStructure

numstructs = 7
test.SomeArray = Allocate(numstructs * Len(SomeType))

For i = 0 To numstructs - 1
    test.SomeArray[i].Description = "" 'Null Dynamic String First!
Next i

test.SomeArray[2].Description = "Drug Dealer"
test.SomeArray[2].Age = 87

For i = 0 To numstructs - 1
Print test.SomeArray[i].Description, test.SomeArray[i].Age
Next i

End Sub

Main

Sleep
Reply
#3
Excellent, at least there is a way :-). Thank you

Now then could I have the following:

Code:
TYPE SomeType
     Description   AS STRING
     ItsID         AS LONG
END TYPE

TYPE DynamicType
     Name          AS STRING
     Number        AS LONG
     DynamicPart   AS SomeType PTR
END TYPE

TYPE CombinedType
     SomeNumber    AS LONG
     SomeName      AS STRING
     WeirdPart     AS DynamicType PTR
END TYPE

Provided I do the proper initialization to everything and allocate what's needed?
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
#4
btw if you have unfixed lenght strings in the udt's then you need to deinit them manually -or you'll have memory leaks.

with yourtype.stringfield = ""
url]http://fbide.sourceforge.net/[/url]
Reply
#5
Yep.

Code:
TYPE SomeType
     Description   AS STRING
     ItsID         AS LONG
END TYPE

TYPE DynamicType
     Name          AS STRING
     Number        AS LONG
     DynamicPart   AS SomeType PTR
END TYPE

TYPE CombinedType
     SomeNumber    AS LONG
     SomeName      AS STRING
     WeirdPart     AS DynamicType PTR
END TYPE

Sub Main
Dim ct As CombinedType

numdt = 9
ct.WeirdPart = Allocate(numdt * Len(DynamicType))

numst = 7
For i = 0 To numdt - 1
    ct.WeirdPart[i].DynamicPart = Allocate(numst * Len(SomeType))
    For j = 0 To numst - 1
        ct.WeirdPart[i].DynamicPart[j].Description = ""
    Next j
Next i

With ct.WeirdPart[3]
.DynamicPart[5].Description = "Menace"
.DynamicPart[2].Description = "Person Unknown"
Print .DynamicPart[5].Description, .DynamicPart[2].Description
End With

With ct.WeirdPart[7]
.DynamicPart[5].Description = "Goody - Two - Shoes"
.DynamicPart[2].Description = "Apathetic"
Print .DynamicPart[5].Description, .DynamicPart[2].Description
End With

End Sub

Main
Sleep
Reply
#6
Always use Callocate() as Allocate() won't clear the contents, so if you do bar->foo = "" (foo being a dyn string), if the contens of foo (the descriptor) aren't 0'ed, the rtlib will try to free an invalid pointer.
Reply
#7
What if I might need to change the number of elements in those PTR things should I use CAlliocate there to? or Allocate if I want what's there to stay preserved so to speak?
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
#8
Reallocate

* points at C realloc() function *
Reply
#9
This raises a question for me too...

I have no experience with pointers. :roll:

Is it possible to allocate memory for these variables on the fly?

Code:
Type ObjType
IdNum as Integer
VertCnt as Integer
FaceCnt as Integer
Num_Surfaces as Integer
Start_Id(1 to 50) as Integer
End_Id(1 to 50) as Integer
Surface_Id(1 to 50) as Integer
Transparent(1 to 50) as BYTE
Textured(1 to 50) as Byte
Collidable(1 to 50) as Byte
RGBA(1 to 25, 3) as Single  
Ambient(1 to 50, 3) as Single
Diffuse(1 to 50, 3) as Single
Specular(1 to 50, 3) as Single
Emission(1 to 50, 3) as Single
Shininess(1 to 50) as Single
Angle_X as Integer
Angle_Y as Integer
Angle_Z as Integer
IsDynamic as Byte
Trans as Vector3DType
TransSpd as Vector3DType
Num_Targets as Integer
CurTarg as Integer
Target(1 to 10) as Vector3DType
TargSpd(1 to 10) as Vector3DType
End Type

Dim Entity(1 to Total_Entities) as ObjType


Basically, all the arrays that have 50 members...
Code:
Start_Id(1 to 50) as Integer
End_Id(1 to 50) as Integer
Surface_Id(1 to 50) as Integer

..need to be allocated to Num_Surfaces instead of 50, but I don't know how to do it. Is it even possible to do that using pointers, and how confusing would the code be? This isn't all either... I have alot of stuff in my program that would benefit from this. If it's even possible that is. Wink
Reply
#10
Yea! In fact that's how the Quake 2 MD2 structs work. Basically you would do this.

Code:
Type Vertex
x As Single
y As Single
z As Single
End Type

Type Tri
verts(2) As Vertex
End Type

Type Obj
numoftris As Integer
tris As Tri Ptr
End Type

Dim tobj As Obj

tobj.numoftris = 100

tobj.tris = Callocate(tobj.numoftris * Len(Tri))\

tobj.tris[10].verts(0).x = 194.415 'Access array using brackets []

You can do that for any struct you have. That's a LOT easier than having id's to arrays if I'm reading your post correctly. Read the above posts too.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)