|
You are right, there is no guaranteed initialization of the RAM (neither on the Hack platform, nor from the Hack OS API, nor from the Jack Language). The only thing that is guaranteed are the values of local variables which are reset to 0 when a function is entered (as part of the VM specification of the FUNCTION command). This is somehow a bit counter-intuitive, since in many languages and platforms this is the opposite (i.e. in Java static class variables and instance variables are automatically initialized to 0 whereas local variables are not initialized - for a good reason).
Your conclusion on how to resolve this is not correct though. You just have to take care of the initialization of variables yourself in the respective Jack code. Your proposal would move the responsibility of the initialization to the Operating system. If you rely on Memory.init or Memory.alloc to take care of that, your code might run correctly with your own OS implementation, but fail with other ones, including the stock OS provided with nand2tetris.
The standard way of handling this in Jack is:
1. for static variables a static function has to handle the initialization. Typically it is called init() (not a fixed convention though). It is then the programmer's responsibility to call that function before using the class. For the OS this is handled by Sys.init which calls all required init routines of the other static OS classes. For own classes, the required init routines are typically called from Main.main.
2. for fields (instance variables) the constructor of the class should initialize the fields.
3. for arrays, code to initialize the array has to be placed where and if required.
|