Stack corruption problem
From: Mani (smani78_at_rediffmail.com)
Date: 08/11/04
- Previous message: Al: "Integrating SIP with J2EE"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: Al: "Integrating SIP with J2EE"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|