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

classic Classic list List threaded Threaded
9 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.
Reply | Threaded
Open this post in threaded view
|

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

ashort
This post was updated on .
not sure where you are going with your Tic Tac Toe game - 2 player game? Play against the computer?  In either case, you might want to think about more efficient storage of the board/pieces.  Then any manipulation or reading of the board will be faster, not to mention a lot less code.

Seems your tic tac toe board is represented as an array of 9 String objects, with each String object being a 1 character string "X", "O", or " " (space).   This is totally logical, as anyone who stares at a tic tac toe board sees 9 squares that contain either X, O, or empty.   Some quick math on storage:  each String in your representation takes up 4 words of heap memory (each word is 16-bits on the Hack computer) - 1 word for the String's internal array address, 1 word for the character in that string, 1 word for the String's internal length field, and 1 word for the String's internal maxLength field.   So your array of 9 squares take up 9 x 4 = 36 words in the heap.

Also, suppose you want to check if someone won the game with 3 in a row?  You would have to have lots of array manipulations to check all 8 ways to win a game, not to mention you have to convert the String to a char using the String's charAt method, for an easy comparison.

So an immediate improvement would be to have an array of 9 chars, rather than an array of 9 Strings. That would take 9 words instead of 36 words, and would avoid the need to call charAt for string comparison. That helps your storage, but will still lead to tedious code to manipulate and read the board.

When I wrote tic tac toe (where you play against the computer), I represented the board in just 2 words.  One of the words is an integer whose set bits 0-8 represent the X pieces and their placement among the 9 squares.  The other word is an integer whose set bits 0-8 represent the O pieces and their placement among the 9 squares.  [You cannot use just 1 word, because each player needs 9 bits of possible placement, for 18 bits total]

Anyway, by using the so called "bitboard" representation, the mechanics of manipulating the board and reading the board become much easier and faster, using bitwise operations.   For example, assuming the bitboard maps to the physical board like this (this is arbitrary):

  0,1,2
  3,4,5
  6,7,8

then how do we check if say, player X won with 3 in a row in the top row?

Assuming piecesPlayerX is a single integer whose set bits represent placement of X pieces, you do this:

if ((piecesPlayerX & 7) = 7)   // 7 = bits 0, 1, 2 turned on:  0000 0000 0000 0111

In reality you might have an array of the 8 possible winning positions (simple bit masks), and loop through all 8 of them. A simple loop of 8 bitmasks where you do one bitwise operation and one equality comparison in each iteration of the loop will likely be much faster than what you can do otherwise.  When building a computer that plays tic tac toe against a human, this speed up is critical.  A further optimization might be to only check certain winning positions based on the last move.


Reply | Threaded
Open this post in threaded view
|

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

ashort
This post was updated on .
In reply to this post by Obij
also, I'm not sure why you need getBoardCopy (returns a copy of the board). It's not even a “deep” copy by the way, because the String entries will be shared among the 2 copies the way you wrote it.  If you were using chars not Strings, then it's not an issue.

But for what it's worth, I built Jack tic tac toe that plays against you and is unbeatable on the highest level. And I never copy the board even once...