An array of typed objects holding it's own arrays? t(1).variables(1)=10

Started by LemonWizard, February 11, 2013, 08:52:05 PM

Previous topic - Next topic

LemonWizard



type object
variables(10000)
variable_names$(10000)
endtype

dim t(10) as object



t(1).variables(1)=10
print t(1).variables(1)



Why does this work, first of all? I thought it wouldn't work.
two... HOW does it work wouldn't it create too much overhead in memory?

Three. THIS is what I Needed for the longest time :D :D :D
Now i can dynamically keep track of so many things in one giant type.

kevin


QuoteWhy does this work, first of all? I thought it wouldn't work.

  Why wouldn't it work ?  Types (structures) can include 1D Integer/Float or Strings arrays in them.   From that Type structure you can then declare , Variables /Arrays / Lists containers.   Or handle them manually through typed pointers. 


Quotetwo... HOW does it work wouldn't it create too much overhead in memory?

  yep.    If a type had only a single field which was an array of 1,000 integers say, then this single array field consumes 1,000 * 4 bytes.   

  ie
PlayBASIC Code: [Select]
  Type Stuff
Ints(1000)
EndType


Dim Collection(1000) as Stuff





  If size of the each type instance is 4000 bytes (See SizeOf), then when all 1000 items are created in the Collection() array, then this would consume at least 1000 * 4000 bytes of memory.   So about 4 meg.   

  If the fields are strings, then you can add on top of that the total length of the string fields.   

PlayBASIC Code: [Select]
  Type Stuff
Strings$(1000)
EndType





  You can query the size (in bytes) of the type structure like so.

 
PlayBASIC Code: [Select]
  Type Stuff
Ints(1000)
EndType

Print SizeOf(Stuff)

SYnc
waitkey



 


LemonWizard

Ohh..


So that will work but a type containing a one dimensional array of another one dimensional array wont work?


kevin


Quote

So that will work but a type containing a one dimensional array of another one dimensional array wont work?


what ?