Stack corruption problem

From: Mani (smani78_at_rediffmail.com)
Date: 08/11/04

  • Next message: Neerja Tyagi: "Re: How to Maintain Sessions in Tomcat 4.1?"
    Date: 10 Aug 2004 22:15:33 -0700
    
    

    Kindly look at the code herebelow.

    The stack AllStack contains the stacks which are empty. In this loop,
    the elements of the AllStack are removed one by one using the pop()
    method, their indexes are found out by equating it with the empty
    stack and based on the Index number, and the corresponding objects are
    filled up into the empty stack.

    But in the for loop, the element in the AllStack gets equated to a
    different stack and corruption in the stacks take place. This is
    observed only when the applet is run using Sun JVM while in Microsoft
    JVM, the stacks are filled up properly.

    import java.util.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class MyStack extends Applet
    {
    public void init()
    {
    Stack [] aStack = new Stack[20];
    Stack AllStack = new Stack();

    // create and populate two stacks in the stack array
    aStack[5] = new Stack();
    for (int i=0; i<10; i++)
    {
    aStack[5].push("5");
    }

    aStack[13] = new Stack();
    for (int i=0; i<10; i++)
    {
    aStack[13].push("13");
    }

    System.out.println("aStack[5] = "+aStack[5]);
    System.out.println("aStack[13] = "+aStack[13]);

    //emptying the stacks and adding the empty stack in the stack AllStack
    for (int i=0;i<10 ; i++)
    {
    String waste = (String)aStack[5].pop();
    waste = (String)aStack[13].pop();

    if (aStack[5].empty())
    {
    AllStack.push(aStack[5]);
    }
    if (aStack[13].empty())
    {
    AllStack.push(aStack[13]);
    }
    }

    for (int iSize = 0 ; iSize <= AllStack.size(); iSize++)
    {
    int iIndex;
    //getting the topmost element (stack) from AllStack
    Stack thisStack = (Stack)AllStack.pop();
    System.out.println("checkpoint 1-aStack[5] ="+aStack[5]);
    System.out.println("checkpoint 1-aStack[13] ="+aStack[13]);

    //getting the Index
    for (iIndex=0; ; iIndex++)
    {
    if ( (null != aStack[iIndex]) && aStack[iIndex].equals(thisStack) )
    {
    break;
    }
    }
    System.out.println("iIndex ="+iIndex);

    //filling up the stack based on the index
    for (int jIndex=thisStack.size(); jIndex < 10; jIndex++)
    {
    thisStack.push(Integer.toString(iIndex));
    }
    System.out.println("checkpoint 2-aStack[5] ="+aStack[5]);
    System.out.println("checkpoint 2-aStack[13] ="+aStack[13]);
    }
    System.out.println("end::::aStack[5] ="+aStack[5]);
    System.out.println("end::::aStack[13] ="+aStack[13]);

    }
    public void start()
    {
    }
    public void paint(Graphics g)
    {
    showStatus("This is shown in the status window");
    }

    }

    Actually, in the problem, if you look at this piece of code

    //getting the Index
    for (iIndex=0; ; iIndex++)
    {
    if ( (null != aStack[iIndex]) && aStack[iIndex].equals(thisStack) )
    {
    break;
    }
    }

    When you run in Microsoft JVM, thisStack is equated to aStack[13]
    whereas in sun JVM, thisStack is equated to aStack[5].

    These stacks work just as pointers during other times, going with the
    fact that if I fill out thisStack, aStack{5]or aStack[13] gets
    automatically filled depending on the JVM.


  • Next message: Neerja Tyagi: "Re: How to Maintain Sessions in Tomcat 4.1?"

    Relevant Pages

    • Re: Higher Order Byte-Code Instructions
      ... order instruction to the Java bytecode? ... that either push or pop instructions on the evaluation stack. ... for extending the expressiveness of the JVM. ... the stack to form part of a generated invokevirtual instruction call sequence ...
      (comp.lang.java.machine)
    • Re: DOLIST with empty list?
      ... To decide to return the empty list, ... But this fundamentally redefines DOLIST/DOSUBS in a manner ... any new results on the stack, you get a list of those ... and I replace each by recalling the variable's contents, ...
      (comp.sys.hp48)
    • Re: what is a stack
      ... >> The JVM can get at the top four elements of the stack more efficiently ... Java does not use Top of stack relative addressing to ... It uses frame relative addressing. ... It might not even put the local variables ...
      (comp.lang.java.programmer)
    • Re: Beat Toudai
      ... of empty pdf pages. ... KPDF got empty little, and empty big, pages. ... But still it and evince ... Execution stack: ...
      (sci.lang.japan)
    • Re: How large is an object reference?
      ... The stack looks identical (at least the elements ... see Section 4.9.6 of the JVM spec. ... Sun are backing off from using jsr/ret to implement ...
      (comp.lang.java.programmer)