Administrator
|
A1) Like the C programming language, Jack has no automatic garbage collection (unlike languages such as Java and Python). So the only way for memory to be freed is for your program to deallocate the memory with deAlloc(). Standard practice is for your class to have a dispose() method. This method needs to be responsible for not just freeing the memory of the object itself, but also any objects, such as your x andy arrays, that are part of the object.
A2) The only variables that are stored on the heap are ones that are explicitly allocated memory there using alloc(). Other variables are either stored in the static memory area or on the stack. Variables stored on the stack are stored in a function's stack frame and are therefore automatically allocated and deallocated as part of the function calling and return routines. There is a fundamental difference between Integer and Array types -- the Array type is an object that has memory allocated on the heap for it when it is created. The Integer is a primitive type that, as noted above, is allocated memory either in the area reserved for static variables or on the stack.
A3) See above.
Even if you do all of this, you may still have heap overflows eventually because Jack does not treat string literals well. Memory gets allocated for them by the compiler, but there is not way to deallocate them when finished. This results in a "memory leak" that is undesirable. The way that most compilers deal with this is allocating memory for string literals as part of the code itself and then replacing references to the string with the associated pointers. Something similar could be done in Jack, although putting the strings on the Heap (don't want to chew up the limited static area with them). But then the compiler has to be a bit more complex and the authors chose to accept the memory leak instead of burdening the students with the added complexity -- the compiler is already challenging enough.
|