So I'm coding a game, and in this game, I need to use a goto to go to a label that is outside of the smaller repeat loop that the goto is in, but is still in the main repeat/while loop. In this situation, would this count as a memory leak or no? Would appreciate any answers anyone has.
That was confusing to follow, but basically any time you jump from within a Repeat/While/For loop to outside that loop, it will cause a leak. (There are convoluted tricks to actually take advantage of this, though.)
It is safest to make the condition as a way to exit the loop(s), then Goto.
Note: It is safe to Goto within the same loop.
Safe:
Repeat <<condition>>
Goto 0
Lbl 0
End
Memory leak:
Repeat <<condition>>
Goto 0
End
Lbl 0
And with all of that said, when the BASIC program exits, it will free up that memory, thankfully (some Assembly programs eat free RAM and you need to clear RAM to get it back).
Z80 Assembly>English>TI-BASIC>Python>French>C>0
Adding on to what Xeda said;
Every time you go another level deeper in your nest you also have to be aware of it. You can’t just jump from one loop to another, except in very limited circumstances. Indending is an easy way to see this:
//no leak
While 1
5+5
While 1
Goto A
4+4
Lbl A
End
Repeat Ans<0
Ans-1-> Ans
End
End
//Still no leak (Notice the Goto and Lbl occurr on the same level of indent)
While 1
5+5
While 1
4+4
Goto A
End
Repeat Ans<0
Lbl A
Ans-1-> Ans
End
End
//This one has a memory leak (Notice Lbl and Goto are not on same level of indent)
While 1
5+5
While 1
Goto A
4+4
End
Repeat Ans<0
Ans-1-> Ans
End
Lbl A
2+2
End
//This memory leak is twice as bad
While 1
5+5
While 1
Goto A
4+4
End
Repeat Ans<0
Ans-1-> Ans
End
End
Lbl A
1+1
You also have to be careful that you don’t jump into a loop that is deeper than you are currently running. For example, this would throw an error:
While 1
5+5
Goto A
While 1
4+4
End
Repeat Ans<0
Lbl A
Ans-1-> Ans
End
End
I’m sure Xeda or I could give a more technical explaination if you wanted it, but I think that these code examples might answer your question well enough
That's a fantastic explanation.
Z80 Assembly>English>TI-BASIC>Python>French>C>0
Thanks! I learned very early on in my programming “career” what memory leaks were, and since I wasn’t skilled enough to build programs without Goto, I learned to make it easy for myself to find them. Nowadays I try to avoid using Lbl and Goto as much as possible, since its hard to have a leak if you don’t have any Goto’s
I'll try explaining this again, and if this is not clear then I'll have to think of another way to ask this question. So I have a program that loops back to the top of the program whenever the player finishes a room so the next room can be load (This is a game with multiple mazes that are stored using matrices into another program), and to get to that program I have to use a goto to basically reload the map again, but this time load the second room because the player has already completed the first room. The goto is located in this if statement:
:If [A] (A,B)=2
:Then
:L+1->L (This basically tells the subprogram with all the matrices what "level" the player is on)
:Delvar SDelvar T (Variables for a collectible that need to be reset in each new room)
:If L (is greater than or equal to) 7 - (If the player has rooms that need to be finished)
:Goto 8 (here is the goto, which goes to a label outside of this if statement.
:End
I guess my question now would be how do I prevent a memory leak without having to rewrite most of the code? If no one can answer I understand. I would seriously appreciate all help, thank you.
The way to do it is instead of using a Goto there, is to make the loop exit, and then jump. I can't really explain it well without knowing your full code and exactly what you are trying to do
I think the If/Then/Goto doesn't follow the indent rule, so yoir code is fine as is. I suspect that your If statements are inside of a bigger loop, and that is what is causing the problem.
(Also, you can post code in [[code]] tags:
[[code]]
blahblah
[[/code]]
Turns into:
blahblah
Z80 Assembly>English>TI-BASIC>Python>French>C>0
That's actually incorrect. Any token or command which requires the use of an End statement pushes another pointer onto the stack. While this includes the obvious While, Repeat, and For loops, it also includes Then. If you reference the page on memory-leaks it says:
Simply put, a memory leak is where you use a Goto/Lbl within a loop or If conditional (anything that has an End command) to jump out of that control structure before the End command is reached
It can be noted, however, that a single line If statement can usually be used with a Goto as it does not have a Then … End block.
Looking at the code that was provided:
//Shortened for my own clarity
If [A](A,B)=2:Then
L+1→L
Delvar SDelvar T
If L≥7:Goto 8
End
If the Lbl 8 is on the same level of the stack as the Goto 8, it will be fine. As it was described, to me it sounds like there is a memory leak here but it really depends on where exactly in the code the label is. For example…
This one would have a memory leak:
While 1
Lbl 8
prgmDRAWMAP
Repeat K
getKey
End
prgmDRAWMAP
If [A](A,B)=2:Then
L+1→L
Delvar SDelvar T
If L≥7:Goto 8
End
End
This one would not have a memory leak:
While 1
If 1: Then
Lbl 8
prgmDRAWMAP
End
Repeat K
getKey
End
prgmDRAWMAP
If [A](A,B)=2:Then
L+1→L
Delvar SDelvar T
If L≥7:Goto 8
End
End
Well, heck. I always thought that was safe; I know when I implemented If/Then/ElseIf/Else in Grammer and Parsnip, If didn't have those problems, unlike with For/Repeat/While, because you don't have to keep track of the start of the condition.
That's really interesting that TI would choose to go that route XD
Z80 Assembly>English>TI-BASIC>Python>French>C>0