0

I am implementing a simple ijvm using a stack in java, however, I encounter a very weird behavior and I have no idea why. So for my stack class I have a constructor

  public Stack(int maxStacksize) {
    maxSize = maxStacksize;
    stack = new Word[maxSize];
    size = 0;
  }

(maxSize, stack and size declared are private variables of the class. and Word is my own class) my push;

  public static void push(Word input) {
    stack[size] = input;
    size++;
  }

These seem fine to me, correct me if I'm wrong! So when the input is BIPUSH 20 BIPUSH 40

I recognize the bipush instruction and push them on the stack per instruction, which works fine, I printed what I'm pushing and it first pushes 20 then 40. However after I push the 40, the 20 in the stack[0] turns into 40 for some reason? I printed the whole stack whenever I push something and so when pushing the 40, the stack turns into [40, 40] instead of [20, 40] and I have no idea why. I also print "size" every time and the size parameter seems correct whenever it's pushing. I know I don't have a check yet for if size is below 0, I'll add that later Any ideas?

4

0 回答 0