"dev42" wrote:
- if "home" is pressed, does gc get called? Yes, but ... not the same as if the app closed correctly? In light of the potential mess of refcnt's.
- what if... only *ONE* area assigns globals and everywhere else accesses them?
- a third point, because there have to be at least 3
- memory will be freed no matter how app was exited. It's matter of good style to clean up after oneself - just like closing open files before exiting a C/Java/Python program ("yes, the OS will take care of it but you do it regardless" - they'd tell you)
- that's fine if it suits your fancy
- point acknowledged
To reiterate what you just said, "passed as an argument" ... specifically emphasizing "sending", right? So, what's better/preferable: ● B(m.loc) ● B(m.loc.x, m.loc.y) ● B(m)? ... when B() is called by A() and A() is most likely being called every time through the main loop!
Well that depends on what exactly are you doing. I would say write whatever seems natural to you at first. Luckily in B/S whatever comes naturally - laziness, do less typing - is also faster. Working with a local variable ("x") is an order of magnitude (> 10x) faster than "dotting" as m.x (regardless whether for global or object member). SImplifying a little bit here:
m.x is just syntax sugar for
m["x"]. Using global functions and local variables is fast, anything that involves a dot - not so much. Don't get all crazy on me now and banish object use completely. Optimize later, you may not need it. (RokuMarkN frowns when i talk about performance, i know 8-) Sorry).
It's usually the loops you get to worry about, where most of the time is spent. The mouthful "loop-invariant expression hoisting" simply means avoid repeating the same work over and over in the loop - move it outside. E.g.
for i = 1 to lineLen
m.loc.x = m.loc.x + dx
m.loc.y = m.loc.y + dy
plot(m.loc.x, m.loc.y)
end for
' loop-invariants moved out, much faster
x = m.loc.x : y = m.loc.y
for i = 1 to lineLen
x = x + dx
y = y + dy
plot(x, y)
end for
So, you're just decrementing your own internal size counter, but never releasing any of the array's individual elements, right? You could, but not with your code, still access s, even when ptr < x.
You got it!
In pull() I should have invalidated the element at the ptr... after returning it :twisted: Ugh! Yeah, temp variable is in order:
function(): m.ptr = m.ptr - 1 : res = m.stk[m.ptr] : m.stk[m.ptr] = invalid : return res : end function
Or of course, i could have avoided re-inventing the wheel and just used roArray.push() and .pop() instead of rolling my own but that wouldn't have allowed me to demonstrate a "memory leak", now would it.
Your demo-code confuses me & is a construct I've not fully accepted ( it's not in my quiver ). How is s.push() or s.pull() even allowable if the stack function requires a parameter? ( Never-mind. It's the formatting. I'm doing the same thing in long format. Not only that, I don't [yet] use the ":" trick in my actual code. ) For clarity, the dim is only called once, right?
Sorry - i don't usually write so dense but in the forum i go to great
lengths to make code
short so it can be easy to see at a glance - preferably without scroll bar at the code box.
- Stack() is just an "object factory" function (or "constructor") - when called, it makes and returns an object you can continue beating at own leisure.
- DIM looks declarative but is in fact executable statement, with
dim stk being thereabout equivalent to
stk = createObject("roArray", n, true). So each time
Stack() is called, new array is allocated and assigned to
stk at that line.
---
(this Reply has been hanging open in my browser for a week so... Submit!)