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.

Obij
     Please could someone assist me with the following issue: I was trying to test some vm files for a jack Tic-Tac-Toe application, but i got the following VM emulator error "A built-in function tried to access memory outside the Heap or Screen range" , the error can be seen in the VM emulator screenshot in the link below:

https://www.dropbox.com/s/etde3zjk6jd4f8j/Screenshot%202020-01-28%2000.14.54.png?dl=0 

   Also, the error occurs when the emulator tries to print an Array element, from the board Array in the drawBoard() method, in my TicTacToeBoard class. What is surprising is that the array, I am trying to print or use is not too big or complex, as it just has 9 elements. Below are the Jack files for my application .(The first class, in the display is the TicTacToeBoard class, while the second, situated at the end is the Main Class.) Please any help would be seriously appreciated, as i have already spent hours on this.Thanks.

/** creates a Tic-Tac-Toe board */

class TicTacToeBoard{
    field Array board; //an Array to be used for board construction
       
        /** constructs a TicTacToe board */
        constructor TicTacToeBoard new(){
           var int i;
           let board= Array.new(9);
           let i = 0;
           while(i < 9){
              let board[i] = " ";
           return this;
           }
           
        }
       
       
       
        /** method draws the empty TicTacToe Board */
        method void drawBoard(){
           //do Output.printString("*** Start of drawBoard method");
           //do Output.println();     /* DEBUG */
           do Output.printString(board[6]);
           do Output.printString("|");
           do Output.printString(board[7]);
           do Output.printString("|");
           do Output.printString(board[8]);
           //do Output.printString(getBoard[7] + "|" + getBoard[8] + "|" + getBoard[9]);
           do Output.println();
           do Output.printString("-+-+-");
           do Output.println();
           do Output.printString(board[3]);
           do Output.printString("|");
           do Output.printString(board[4]);
           do Output.printString("|");
           do Output.printString(board[5]);
           //do Output.printString(getBoard[4] + "|" + getBoard[5] + "|" + getBoard[6]);
           do Output.println();
           do Output.printString("-+-+-");
           do Output.println();
           do Output.printString(board[0]);
           do Output.printString("|");
           do Output.printString(board[1]);
           do Output.printString("|");
           do Output.printString(board[2]);
           //do Output.printString(getBoard[1] + "|" + getBoard[2] + "|" + getBoard[3]);
           return;
        }
       
        /** method returns a copy of the board */
        method Array getBoardCopy(){
           var Array boardCopy;
           var int i;
           let i= 0;
           let boardCopy= Array.new(9);
         
           while(i< 9){
              let boardCopy[i]= board[i];
           }
           return boardCopy;
        }
}

/**Tests the Tic-Tac-ToeBoard class */
class Main{
    function void main(){
           var TicTacToeBoard tTTBoard;
           let tTTBoard= TicTacToeBoard.new();
           do tTTBoard.drawBoard();
           return;
        }
}

Reply | Threaded
Open this post in threaded view
|

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

WBahn
Administrator
The problem is that your constructor is not doing what you think it is doing.

Go through and carefully format your code to match what you have actually written, not what you wanted to write.

You have a couple of issues with this code, but let's get this one taken care of first.
Reply | Threaded
Open this post in threaded view
|

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

Obij
@WBahn, thanks for the fast response. And please sorry for imposing, but could you give me more details? I have looked at my constructor and I can't see anything wrong with it.
Reply | Threaded
Open this post in threaded view
|

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

WBahn
Administrator
That's because you are just looking at it and seeing what you WANT to see, not what is actually there. This is very common in us humans because we know what we meant to write, and so when we review it we tend to see what we believe we wrote, whether it is what we actually wrote or not.

I recommend that you format it using the Allman indentation style

Present that here (just the constructor) and if you don't see where the problem is in the process I will point it out to you.
Reply | Threaded
Open this post in threaded view
|

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

Obij
I looked at it again and I saw one error, which was that I included the return statement inside the while block.
However, when i placed the return statement outside the while block, compiled and ran the code on the VM, the program  seemed to have entered an infinite loop: it never gets to the drawBoard() method.

Also from the link, the Allman Style seems to be all about placing the place associated with a control statement on the next line, thus, if used on my constructor (and with the return statement in the new place), the code should be as in below. (However, I still have the problem of the infinite loop mentioned above). Thanks.

constructor TicTacToeBoard new()
{
           var int i;
           let board= Array.new(9);
           let i = 0;
           while(i < 9)
           {
              let board[i] = " ";
           }
           return this;
           
}
Reply | Threaded
Open this post in threaded view
|

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

WBahn
Administrator
So you spotted one of the errors. Hopefully you can see how formatting the code as written makes that error pretty evident.

As for the second error, let's bench check what you have written.

01 constructor TicTacToeBoard new()
02 {
03     var int i;
04    
05     let board= Array.new(9);
06     let i = 0;
07     while(i < 9)
08     {
09         let board[i] = " ";
10     }
11     return this;
12 }

What is 'i' after you execute line 06?
Does the while() test pass or fail in line 07?
What is 'i' after you execute line 09?
Where do you go after executing line 09?
What is 'i' when you get there?

Again -- focus on what you actually wrote, not what you wanted to write.

Reply | Threaded
Open this post in threaded view
|

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

Obij
Thanks, I 've seen the error (i forgot to include a statement inside the while loop, to increment the while loop index!), very stupid of me. Also, i 've corrected it and tested the code and it is working as expected.

Thanks, again. I 'm grateful.