Qbasicnews.com
April 12, 2021, 07:57:06 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Dynamic Arrays in User Defined Types  (Read 8026 times)
lkd85
Member
*
Posts: 63


« Reply #15 on: March 09, 2005, 12:34:33 PM »

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)
Logged
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #16 on: March 10, 2005, 02:09:09 AM »

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.  :Huh:


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

Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #17 on: March 10, 2005, 02:32:02 AM »

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
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #18 on: March 10, 2005, 02:49:27 AM »

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
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #19 on: March 10, 2005, 02:54:53 AM »

Code:

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

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #20 on: March 10, 2005, 03:22:18 AM »

Hmmm.... This wont run.  :Huh:


Code:
Type Surface_Properties
 RGBA(3) as Single  
 Spec(3) as Single  
End Type

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

Type Model_Type
   Num_Verts as Integer
   Num_Faces as Integer
   Vec as Vector3DType
   Surf as Surface_Properties
end Type


Dim Model(1 to 10) as Model_Type Ptr


Model(1)->Num_Verts = 100
Model(1)->Vec = allocate(Model(1)->Num_Verts * Len(Vector3DType))
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #21 on: March 10, 2005, 03:31:19 AM »

Ptr.

Code:
Vec as Vector3DType ptr


You forgot to declare as Ptr
 Cheesy
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #22 on: March 10, 2005, 03:38:26 AM »

He he he... now this crashes. :roll:


Code:
Type Surface_Properties
 RGBA(3) as Single  
 Spec(3) as Single  
End Type

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

Type Model_Type
   Num_Verts as Integer
   Num_Faces as Integer
   Num_Surfaces as Integer
   Vec as Vector3DType Ptr
   Surf as Surface_Properties ptr
end Type


Dim Model(1 to 10) as Model_Type ptr


Model(1)->Num_Verts = 100
Model(1)->Num_Surfaces = 3
Model(1)->Vec = Callocate(Model(1)->Num_Verts * Len(Vector3DType))
Model(1)->Surf = Callocate(Model(1)->Num_Surfaces * Len(Surface_Properties))
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #23 on: March 10, 2005, 03:46:11 AM »

You forgot to allocate model_type ptr

Code:
Dim Model(1 to 10) as Model_Type ptr

for i = 1 to 10
   model(i) = allocate(Len(Model_type))
next i
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Dr_Davenstein
Na_th_an
*****
Posts: 2052


« Reply #24 on: March 10, 2005, 03:49:28 AM »

Oh, Thanks!!! I didn't know I had to do that too.  :rotfl:

EDIT:

It seems to work good and that's a nice clean structure. Thanks again.  Tongue
Logged
lkd85
Member
*
Posts: 63


« Reply #25 on: March 10, 2005, 08:34:19 AM »

BTW, for Matrices you can also use a 2D pointer array.  This is what I'm using in my bootleg 3d math library

Code:

#define MATRIX4X4 Single Ptr Ptr

'PtrArray2D part of Extra Lib (library of extra functions I'm working on.)
Function PtrArray2D(Byval rows As Integer, Byval cols As Integer,Byval bytes As Integer) As Any Ptr Ptr
         Dim tempptr As Any Ptr Ptr
         tempptr = Callocate(rows * bytes)
         For i = 0 To rows - 1
             tempptr[i] = Callocate(cols * bytes)
         Next i
         PtrArray2D = tempptr
End Function

Dim global As MATRIX4X4

global = PtrArray2D(4, 4, Len(Single))

global[1][1] = 24.653
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!