Author Topic: question about dynamic arrays of types  (Read 2844 times)

Offline siatek

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
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

glbasic roxx !!!

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3366
  • Integrated Brain
    • View Profile
Re: question about dynamic arrays of types
« Reply #1 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...


MrTAToad

  • Guest
Re: question about dynamic arrays of types
« Reply #2 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.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2621
    • View Profile
Re: question about dynamic arrays of types
« Reply #3 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.
« Last Edit: 2012-Oct-23 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1634
  • Particle Hawk
    • View Profile
Re: question about dynamic arrays of types
« Reply #4 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.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline siatek

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
Re: question about dynamic arrays of types
« Reply #5 on: 2012-Oct-23 »
Thanks for replies ...
Cheers
glbasic roxx !!!

MrTAToad

  • Guest
Re: question about dynamic arrays of types
« Reply #6 on: 2012-Oct-25 »
No problem!

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10708
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
question about dynamic arrays of types
« Reply #7 on: 2012-Dec-29 »
But technically dim cxx]0] also frees all child item arrays.