THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 15/08/2006 at 02:52, xxxxxxxx wrote:
I don't know too much about the internal structure of COFFEE so this is mainly speculative (but based on most languages).
Basically, there are two types of memory allocation: static and dynamic. Static memory is what is compiled into a program and is local to the program's data segment. While the program is executing, Dynamic memory is what is allocated from the system heap to hold stacks and 'memory' structures (classes, arrays, and so on).
Stacks store 'activation frames' for functions which hold information about arguments and local variables for the function call. It is called a stack because it acts like a stack of cards - when a function is called, it creates an activation frame on the top of the stack (pushed). When the function returns, the activation frame is removed from the top of the stack (popped). Simply put, think of the stack as a shifting tower of function calls where at the bottom of the stack sits main(). As calls return, they are 'popped' off of the stack further and further down until eventually main() returns and the program exits.
Also, there are more volatile stack elements, such as your var B, which only exist within a context within the called function. This usually extends to anything inbetween {} including loops and conditionals.
Like main(), global variables and local main() variables are sitting on the stack until execution ends. They always exist.
A solution to these is to A) avoid global variables whenever possible and B) break down routines so that a series of functions are called and return in a way that local variables are partitioned judiciously only for the work for which they are needed and go away in a timely manner.
Another option is dynamical memory allocation which puts data in the heap rather than the stack. Here's where my understanding of COFFEE is lacking. When you new a class, one would expect the new class instance to be taken from the heap. Remember that there is a difference between a stack 'pointer' to the class and the class itself. The pointer only occupies as much stack as a memory address value requires while the class instance would be the sum of all of its member variables. The problem with COFFEE is that it doesn't have 'pointers'. One must assume that when you do this:
var object = new(myClass);
object is a local variable on the stack and a 'pointer' to the myClass instance and the myClass instance is in memory.
Robert