Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / What is the lifetime of arrays and variables?

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Dec 2016 12:59
I've been away from DBC and DBPro for the last few years, but I was recently asked to make a Mac version of an old scoreboard program I'd done in DBC and so I thought I'd give AppGameKit a try. One thing that confuses me (coming after 4+ years of working with C++) is that variables have a much longer lifetime than I expected. For example in this code:



I thought IntArray would be recreated as an empty array at the start of each loop, leaving the final array length as 0, but instead there is only one IntArray and it just gets longer and longer. It's as though I had written this instead:


The same issue is also true of variables, I noticed that "ArrayLength" can be declared inside the for-loop and still accessed after the for-loop has finished.

Can anyone tell me what's the actual lifetime of variables and arrays, and at what point do they get cleaned up (if at all)?
If an array is created inside a function, should UnDim be called before the function exits/ends?
Finally, if you create a variable of a user type and that user type contains an array, how do you handle it's cleanup?
We spend our lives chasing dreams. Dark Basic lets us catch some of them.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Dec 2016 13:52
Variables created outside of a function last for the duration of the application
Variables created inside a function last for the duration of the function (unless you declare them as global)
Arrays created anywhere (including inside a function) last for the duration of the application

To minimise array memory usage or reset an array you should use

myArray.length = -1

UnDim is a legacy command.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
damothegreat
User Banned
Posted: 20th Dec 2016 13:57 Edited at: 20th Dec 2016 13:59
Or do a cleanup manually

By using

for a=1 to intarry.length
IntArry.remove ( 0 )

next

Just loops through the whole length of the array - deleting the 1st one in the queue everytime

Or something along those lines

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Dec 2016 14:08
Thanks for the info, that's useful to know. Now I just have to restructure a chunk of my code...
We spend our lives chasing dreams. Dark Basic lets us catch some of them.
RobbSnow
8
Years of Service
User Offline
Joined: 14th Oct 2015
Location:
Posted: 22nd Dec 2016 16:25
Since you have been working in C++ for years it might be fun to rewrite it in Tier 2 (C++)

Login to post a reply

Server time is: 2024-05-18 08:21:32
Your offset time is: 2024-05-18 08:21:32