CSC403: More Storage

Contents [0/7]

More Storage: Variables and fields [1/7]
More Storage: Is this correct? [2/7]
More Storage: Is this correct? [3/7]
More Storage: Is this correct? [4/7]
More Storage: Is this correct? [5/7]
More Storage: Is this correct? [6/7]
More Storage: Is this correct? [7/7]

(Click here for one slide per page)


More Storage: Variables and fields [1/7]

Storage

Sharing

Initialization

See XFields.java

Here is the state just before the main function ends:

trace-026-XFields_f_14

More Storage: Is this correct? [2/7]

    public int size () {
        return size (root, 0);
    }
    private static int size (Node x, int sz) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left, sz);
        size (x.right, sz);
        return sz;
    }

Recall this broken version of size

Mistake here is to think of sz as shared among the function calls.

Here is a trace of an execution.

More Storage: Is this correct? [3/7]

    public int size () {
        return size (root);
    }
    private static int sz = 0;
    private static int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

Is this correct?

More Storage: Is this correct? [4/7]

    public int size () {
        return size (root);
    }
    private static int sz = 0;
    private static int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

Is this correct?

Only if you call size exactly once, no matter which instance you call it on. If size() is called more than one time (regardless of the instance of the containing class) it gives the wrong answer.

Here is a trace of an execution.

More Storage: Is this correct? [5/7]

    public int size () {
        return size (root);
    }
    private int sz = 0;
    private int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

A variation without static. Still wrong: if you call size() more than once on any particular object instance, the value returned will be wrong for that instance!

Here is a trace of an execution.

More Storage: Is this correct? [6/7]

    public int size () {
        sz = 0;
        return size (root);
    }
    private static int sz;
    private static int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

Is this correct?

More Storage: Is this correct? [7/7]

    public int size () {
        sz = 0;
        return size (root);
    }
    private static int sz;
    private static int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

Is this correct?

Only if your code is single threaded. If two threads call size() on any instance of the containing class the same time, the sz field could become a mess.