A built-in function tried to access memory outside the Heap or Screen range

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

A built-in function tried to access memory outside the Heap or Screen range

helios77
This is the code of a program which calculates the magnitude and phase of a complex number. The error it's showing is A built-in function tried to access memory outside the Heap or screen range why is this
ComplexNumers.jack
Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

dolomiti7
A program in jack needs to have a class Main with a static function main(). The VM Emulator will first call Sys.init (either the built-in routine, or from Sys.vm if that file is included in the program folder) which will - after initializing the OS classes - call Main.main. Your posted example contains a main() routine inside the ComplexNumers (consistent typo, missing "b" in filename and class name btw) instead. Jack is not Java..., this will not work.
Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

helios77
Thank you for letting me know
Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

dolomiti7
In reply to this post by helios77
Another thing: you don't follow the OS API properly (again):

Output.println()...
Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

helios77
This post was updated on .
In reply to this post by helios77
Can I find the phase of a complex number in jack??
Main.jack
Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

dolomiti7
It would be helpful, if you explain your issue in more detail.

In either case some general comments regarding your code:
1. The code works now, but this is not how an implementation should look like. The Main class is supposed to start the application and not to contain all the program logic. From a design perspective the complex numbers class should stay separated and you just create an instance of such a class inside the Main class (or in more complex programs in another appropriate class). So what I meant with my first reply was to create a class Main with a function main (required), but other than that leave the program logic for your complex class as it was.
2. The Jack language supports only integers. You are using real numbers (i.e. a literal 3.14 in your code... it seems that the Jack compiler is ignoring this and just dividing by 3). Please make sure that you have properly read and understood the language specification. Functionality for floating point would have to be implemented by yourself (or you have to use one of the existing implementations posted in this forum).
3. You can explicitly call Math.multiply and Math.divide as you did in your code. However, the symbols "*" and "/" are supposed to be and will be converted into such calls by the compiler. It will make the code much more readable if you use the symbols instead. Be aware that Jack doesn't specify any operator's precedence, so parenthesis has to be used to ensure the right calculation order.
4. You could potentially calculate integer approximations by doing calculations in 2 steps:
i.e. ...(x * 314) / 100... Or use some fixed-point arithmetic. However, given the limited range of 16-bit signed integers, there is a high chance that you would run into overflows or underflows - and for sure accumulated rounding errors.

Reply | Threaded
Open this post in threaded view
|

Re: A built-in function tried to access memory outside the Heap or Screen range

kamadodesu
Nice i need a help. do you have any idea as to how i should strucutre to make a calculator using jack langaugae with statistical functions like mean median mode? if possible can you give a code(this is not possible) but i need an idea as to haow to initialise it basically and structuring of the program? can you help me with it @dolomiti7