4

I am doing a project for nand2tetris. We write a program in Jack and test it on VMEmulator. The class looks like this:

class List {
    field int data;
    field List next;

    /* Creates a new List object. */
    constructor List new(int car, List cdr) {
        let data = car;
        let next = cdr;
        return this;
    }

    /* Disposes this List by recursively disposing its tail. */
    method void dispose() {
        if (~(next = null)) {
            do next.dispose();
        }
        // Use an OS routine to recycle the memory held by this object.
        do Memory.deAlloc(this);
        return;
    }

    /* Prints the list*/

    method void print() {
        do Output.printString(" -> ");
        do Output.printInt(data);
        if (~(next =  null)) {
            do next.print();
        }
        return;
    }

    /* Inserts the argument in the right position of the list (ascending order)*/
    method void insertInOrder(int ins){
        var List prev, curr, insert;
        let prev = this;
        let curr = prev.getnext();
        while (ins > prev.getdata()){
            if (ins < curr.getdata()){
                let insert = List.new(ins, curr);
                do prev.setnext(insert);
            }
            else{
                let prev = prev.getnext();
                let curr = prev.getnext();
            }
        }
        return;
    }

    /* Searches the argument in the list, if found, it returns the corresponding List object*/
    method List find(int toFind){
        var List temp;
        var List equal;
      var boolean found;
        let temp = this;
        let found = false;
        while (~(next = null)){
            if(toFind = temp.getdata()){
                let equal = temp;
                let found = true;
            }
            let temp = temp.getnext();
        }
        if (found){
            return equal;
        }
        else{
            return null;
        }
  }

    method List getnext(){
        return next;
    }

    method void setnext(List object){
        let next = object;
        return;
    }

    method int getdata(){
        return data;
    }

}

It has one private variable data and a pointer next. So I wrote getter and setter method to return those values. Other methods are fine only the getdata()method is incorrect. When it runs through the VMEmulator, it shows the error Out of segment space in List.getdata.3. This shows in the VMEmulator.

0function    List.getdata0
1push        argument0
2pop         pointer0
3push        this 0
4return

the error is at the 4th line return. When I change the Jack code, the same error is still at the 4th line.

What exactly is the problem in my getter method?

TGrif
  • 5,725
  • 9
  • 31
  • 52
Alex Ding
  • 41
  • 4
  • 1
    Hello, i'm struggling with the same error message and I can't wrap my head around it. Did you find any solution @alex ? – El-Cortez Feb 27 '22 at 16:31

3 Answers3

2

When you run a VM program on the VMEmulator you must first manually set the pointers to the various segments, otherwise you may get an "Out of segment space" error. To understand the necessary settings, look at what the corresponding .tst file does. An alternative method is to insert the proposed code inside a function, since the function call automatically makes this type of setting.

Koci Erik
  • 21
  • 4
  • I don't understand this answer much, would you be able to expand on it? In the case of this assignment there are no .tst files, as we are writing our own game in the Jack language. Also, what do you mean to put the proposed code inside a function? as far as I can see it's been put inside a method, is that not somewhat equivalent? many thanks! – JustABeginner Dec 18 '21 at 19:19
0

You can get this error when you try to access member data of an object which is not constructed. Could it be that the List cdr in the constructor was not properly constructed?

Løiten
  • 3,185
  • 4
  • 24
  • 36
0

Leaving an answer for future nand2tetris-ers.

I was getting this error because I made a mistake in the VM code generated by my compiler. Specifically, when I was emitting a function command, I was not correctly specifying the number of local variables. This resulted in LCL being initialized incorrectly, so when a pop local 0 command was executed, the VM emulator gave the out of segment space error because there was nowhere to put local 0, because it thought that the function had no local variables.

Paul Batum
  • 8,165
  • 5
  • 40
  • 45