Qbasicnews.com
Dynamic Arrays in User Defined Types - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Dynamic Arrays in User Defined Types (/thread-6369.html)

Pages: 1 2 3


Dynamic Arrays in User Defined Types - Dr_Davenstein - 03-09-2005

So, I could do it like this?


Code:
Type ObjType
Num_Surfaces as Integer
Start_Id as Integer ptr
End_Id as Integer ptr
Surface_Id as Integer ptr
End Type

Dim Entity(1 to Total_Entities) as ObjType

Sub LoadOBJ(Entity as ObjType)

'Get Entity.Num_Surfaces from file...
  Entity.Start_Id=Callocate(Entity.Num_Surfaces*Len(Entity.Start_Id))
End Sub

Then when I want to use the new array in a sub...

Code:
Sub Draw_Entity( Entity as ObjType)

For S = 1 to Entity.Num_Surfaces
  For P = Entity.Start_Id[S] to Entity.End_Id[S]
    
  Next
Next
End Sub

Does that look right?


EDIT:
Ok, that seems to work good, but what about these?

Code:
RGBA(1 to 50, 3) as Single ''heh oops!
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

How do I allocate a matrix in this method?


Would I just do something like this?


Code:
Type Surface_Properties_Type
RGBA(3) as Single ''heh oops!
Ambient(3) as Single
Diffuse(3) as Single
Specular(3) as Single
Emission(3) as Single
End Type


Type ObjType
Num_Surfaces as Integer
Start_Id as Integer ptr
End_Id as Integer ptr
Surface_Id as Integer ptr
Surface_Properties as Surface_Properties_Type ptr
End Type

Dim Entity(1 to Total_Entities) as ObjType


Then....


Code:
Some_Sub(Entity as ObjType)

Entity.Surface_Properties =  Callocate(Entity.Num_Surfaces*Len(Entity.Surface_Properties))

End Sub


Some_Other_Sub(Entity as ObjType)

Entity.Surface_Properties[S].RGBA(1) = 'Get From File

End Sub



It's kinda hard to get used to... Is it even right?


Dynamic Arrays in User Defined Types - lkd85 - 03-09-2005

Pretty much, although you need to subtract 1 from the Num_Surfaces

Code:
For S = 1 to Entity.Num_Surfaces  - 1



Dynamic Arrays in User Defined Types - Dr_Davenstein - 03-09-2005

One more question... Tongue

Do I need to deallocate these, or will it be automatic? I've tested a few things and I didn't notice any memory leaks... at least this Ram-Booster thing I'm using didn't report any...

Is it automatic deallocation when the program ens then?


Dynamic Arrays in User Defined Types - Sterling Christensen - 03-09-2005

Quote:Is it automatic deallocation when the program ens then?
Windows might do that, but I'm pretty sure you're supposed to deallocate them.


Dynamic Arrays in User Defined Types - MystikShadows - 03-09-2005

All this raised another question. Here's a piece of code:

Code:
TYPE SomeType
     ThisField  AS LONG
     ThisOtherField AS STRING  
END TYPE

' Static array for the sake of test, but
' Question would apply to dynamic arrays
' declared like this too.
DIM ThisArray(1 to 100) AS SomeType
DIM Sorted(1 to 100)    AS SomeType

' Assuming I have a function I did called SortArray()
Sorted = SortArray(ThisArray())

Can I do this? Or, do I need to pass the address of ThisArray like with the @ symbol ?


Dynamic Arrays in User Defined Types - lkd85 - 03-09-2005

You need to pass the address. Have the function return a pointer. Include crt.bi with your program and use MemCpy to copy the return pointer into the new array.

Code:
TYPE SomeType
     ThisField  AS LONG
     ThisOtherField AS STRING
END TYPE

'For MemCpy
'$Include: "crt.bi"

' Static array for the sake of test, but
' Question would apply to dynamic arrays
' declared like this too.
DIM ThisArray(1 to 100) AS SomeType
DIM Sorted(1 to 100)    AS SomeType

' Assuming I have a function I did called SortArray()
MemCpy @Sorted(1), SortArray(ThisArray()), 100 * Len(SomeType)



Dynamic Arrays in User Defined Types - Dr_Davenstein - 03-10-2005

I was trying to rebuild the gl program I made using pointers and I ran into trouble. I made this for a small example... Can anyone show me what I'm doing wrong? I'm not able to access those Callocated arrays from main. :???:


Code:
Type Vector3DType
   X as Single
   Y as Single
   Z as Single
End Type

Type Main_Type
   Indices as Integer
End Type

Type Alloc_Type
   Arr(3) as Vector3DType
End Type

Declare Sub Make_Vals( mArray as Main_Type, aArray as Alloc_Type ptr)  
Declare Sub Print_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
Declare Sub Kill_Vals(aArray as Alloc_Type ptr )
  
Dim Main_Array(1 to 10) as Main_Type
Dim Alloc_Array(1 to 10) as Alloc_Type Ptr



   Make_Vals Main_Array(1), Alloc_Array(1)
   Print_Vals Main_Array(1), Alloc_Array(1)


sleep 500
CLS


   Dim aArray as Alloc_Type ptr
   aArray = @Alloc_Array(1)
  
   For i = 1 to Main_Array(1).Indices
      Print aArray[i].arr(1).X
      Print aArray[i].arr(1).Y
      Print aArray[i].arr(1).Z
   Next



Sleep 500
Kill_Vals Alloc_Array(1)




Sub Make_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
   mArray.Indices = 100
   aArray = Callocate(mArray.Indices * Len(Alloc_Type))
  
   For i = 1 to mArray.Indices
      aArray[i].arr(1).X = rnd*3
      aArray[i].arr(1).Y = rnd*3
      aArray[i].arr(1).Z = rnd*3
   Next
End Sub


Sub Print_Vals( mArray as Main_Type, aArray as Alloc_Type ptr )
   For i = 1 to mArray.Indices
      Print aArray[i].arr(1).X
      Print aArray[i].arr(1).Y
      Print aArray[i].arr(1).Z
   Next
End Sub


Sub Kill_Vals(aArray as Alloc_Type ptr )
   Deallocate(aArray)
End Sub



Dynamic Arrays in User Defined Types - relsoft - 03-10-2005

A better way is to do away with arrays and use straight pointers.

ie:

Code:
type vector
   x as single
   y as single
   z as single
end type

type model
    numverts as integer
    vertex as vector
end type

dim vb as model ptr

vb->numverts = 5000
vb->vertex = allocate(vb->numverts *LEN(vector))

'to access each element...

for i = 0 to vb->numverts -1
     vb->vertex[i].x = rnd
     vb->vertex[i].y = rnd
     vb->vertex[i].z = rnd
next i

deallocate vb->vertex
deallocate vb

That way, you're treating the pointer like a structure


Dynamic Arrays in User Defined Types - Dr_Davenstein - 03-10-2005

Ok, but what if you wanted to have vb as an array of models? How do you access that structure in main?


Like this...
Code:
Dim vb(1 to Num_Models) as model ptr



Dynamic Arrays in User Defined Types - relsoft - 03-10-2005

Code:
vb(vbindex)->verterx[vertexindex].x