Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Global vs Local Variables

Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
06-02-2005 00:34
Are local variables always allocated in memory, or are they only allocated and deallocated in their scope / event?
_____________________
Hiro Pendragon
------------------
http://www.involve3d.com - Involve - Metaverse / Emerging Media Studio

Visit my SL blog: http://secondtense.blogspot.com
Jon Marlin
Builder, Coder, RL & SL
Join date: 10 Mar 2005
Posts: 297
06-02-2005 07:12
From: Hiro Pendragon
Are local variables always allocated in memory, or are they only allocated and deallocated in their scope / event?


In general, there are two ways to handle this. One way is to allocate local variables on the stack, which means they will go away. Alternatively, the compiler can allocate them in global memory space, but even in that case you can't assume they won't change. An obvious memory optimization for the compiler to take is to share these "local" memory locations across multiple contexts that don't end up calling each other.

- Jon
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
06-02-2005 15:39
From: Jon Marlin
An obvious memory optimization for the compiler to take is to share these "local" memory locations across multiple contexts that don't end up calling each other.

LSL does not optimize anything.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
06-02-2005 18:32
LSL is not so different from any other sequential programming language.

Local variables are allocated on the stack. However, there is a non-zero amount of code that goes in the text segment for initializing that variable. Kex did a study, and posted these findings in 2003, I think. I don't think the results have changed much.
_____________________
--
~If you lived here, you would be home by now~
Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
06-02-2005 23:24
So then are the local variables removed from the stack when their scope ends, or are they on a global stack and stay there forever?
_____________________
Hiro Pendragon
------------------
http://www.involve3d.com - Involve - Metaverse / Emerging Media Studio

Visit my SL blog: http://secondtense.blogspot.com
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
06-03-2005 01:44
OK, I would be astounded if it were the latter! Normally the stack frame should be allocated and released. If you don't do that as the compiler writer, you'd probably have to jump through a thousand hacky hoops to make the following legal syntax possible...

CODE
if (flag == TRUE) {
integer x = 5;
...
} else {
integer x = 3;
...
}

...since you'd have to figure out while of the 'x's on the stack referred to the current local.

/esc
_____________________
http://slurl.com/secondlife/Together