Simple question: Passing local variables by address: Is it safe?
andrewsi
Posts: 59
I am assuming that local variables in a given method live in a fixed location (for the lifetime of the method, anyway) on the stack of the running object, and that they can be passed safely by address to methods in another object (and modified by them), as long as the called method runs in the same cog as the caller.
It seems reasonably safe to assume that doing this to a different cog would be a very bad idea, since there's no guarantee that the passed address would still be valid for that local variable at any point in the future once the call stack has unwound.
If I'm wrong about any of this, please correct me, but all the formal documentation and books on the Prop seem to focus solely on the use of global variables for passing info between objects and doesn't really discuss the safety (or not) of passing local vars to subroutines. I get why using globals is desirable for passing info between cogs (and I'm doing that too), but in my particular project, turning all the local vars into globals would make the list of globals unnecessarily long and unwieldy. The temporary local variables really only need to exist in the context of the method they pertain to, and the synchronous submethods they call which are all running as part of the same cog.
Thanks, and hopefully that wasn't too difficult to parse.
Andy
It seems reasonably safe to assume that doing this to a different cog would be a very bad idea, since there's no guarantee that the passed address would still be valid for that local variable at any point in the future once the call stack has unwound.
If I'm wrong about any of this, please correct me, but all the formal documentation and books on the Prop seem to focus solely on the use of global variables for passing info between objects and doesn't really discuss the safety (or not) of passing local vars to subroutines. I get why using globals is desirable for passing info between cogs (and I'm doing that too), but in my particular project, turning all the local vars into globals would make the list of globals unnecessarily long and unwieldy. The temporary local variables really only need to exist in the context of the method they pertain to, and the synchronous submethods they call which are all running as part of the same cog.
Thanks, and hopefully that wasn't too difficult to parse.
Andy
Comments