GLBasic forum

Main forum => FAQ => Topic started by: siatek on 2012-Oct-22

Title: question about dynamic arrays of types
Post by: siatek on 2012-Oct-22
Hi a i have a little question:

here is an example code:

Code (glbasic) Select

TYPE TMap

position AS TVec2d
width
height

layers[] AS TLayer

  FUNCTION Create:

DIM self.layers[10]

FOREACH layer IN self.layers[]
layer.Create()
NEXT

ENDFUNCTION

FUNCTION Destroy:

FOREACH layer IN self.layers[]
layer.Destroy()
NEXT

DIM self.layers[0]

ENDFUNCTION

ENDTYPE // TMap

//////////////////////////////////////

TYPE TLayer

position AS TVec2d

entities[] AS TEntity

isVisible

FUNCTION Create:

self.Destroy()

ENDFUNCTION


FUNCTION Destroy:

DIM self.entities[0]

ENDFUNCTION


FUNCTION AddEntity: entity AS TEntity

DIMPUSH self.entities[], entity

ENDFUNCTION

ENDTYPE // TLayer


and now is the question:

is function Destroy in TYPE TMap shoud be:

Code (glbasic) Select
FUNCTION Destroy:

FOREACH layer IN self.layers[]
layer.Destroy()
NEXT

DIM self.layers[0]

ENDFUNCTION



or

Code (glbasic) Select
FUNCTION Destroy:

DIM self.layers[0]

ENDFUNCTION


is good enought ??

regards

Title: Re: question about dynamic arrays of types
Post by: mentalthink on 2012-Oct-22
For dealloc array from memory you have to use dim array_Name[0]... If I don“t remenber bad, if you do dimpush, only delete the index but the array continue in memory...

Title: Re: question about dynamic arrays of types
Post by: MrTAToad on 2012-Oct-22
Technically, using DIM xxx[0] should de-allocate everything, although it may only happen when the computer has enough free time to do so.

However... if you have been using files, loading specific graphics/music etc that you want to have removed when the type is destroyed, then you would need the FOR/NEXT loop to close file handles and so on.

You may also like to make sure that you always deallocate arrays in your Destroy function anyway to maintain symmetry with the your constructor and to make sure everything does get de-allocated.
Title: Re: question about dynamic arrays of types
Post by: bigsofty on 2012-Oct-22
I agree with Mr. T. "Destroy", is the better of the two. Both will work but Destroy(1st method) gives you a more finite method destruction than just removing stuff from memory. Changing global counters, informing other sub types of the destroy event etc.
Title: Re: question about dynamic arrays of types
Post by: Hemlos on 2012-Oct-23
Killing each layer makes sense, certainly for insurance of being cleared.
If the list is massive it might take more time to complete the function.

Some test cases:

If the typical case of use is in fact a small array, regardless of useage occurances, then the first case should be sufficient.

In a case where the array might contain massive amounts of data, and you want your program to run as fast as possible, then the second case might be more desireable.
Title: Re: question about dynamic arrays of types
Post by: siatek on 2012-Oct-23
Thanks for replies ...
Cheers
Title: Re: question about dynamic arrays of types
Post by: MrTAToad on 2012-Oct-25
No problem!
Title: question about dynamic arrays of types
Post by: Kitty Hello on 2012-Dec-29
But technically dim cxx]0] also frees all child item arrays.