Error in Copy Constructor(stack)

HI

This is a Copy Constructor function, using stack
when i run this function,(other functions run okay)the program stop working.
I revise the function several times and i didn't get anything
So i hope some one can help me.

First this is the function for stack


1
2
3
4
5
6
7
8
9
10
11
12
13
struct NodeLinked
{
	int info;
	NodeLinked *link;
};

class MySatck
{
	int Max,st;
	int *List;
	NodeLinked *StackTop ;

};



And this is Copy Constructor function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
MySatck::MySatck(MySatck & obj1)
{
	Max=obj1.Max;
	st=obj1.st;
	List=new int [Max];

	if(st!=0)
	{
		for(int i=0; i<=st; i++)
			List[i]=obj1.List[i];
	}
	
	if(obj1.stackTop==0)
		stackTop=0;
	else
	{
		MySatck objT;

		while(!obj1.isLinkedEmpty())
		{
			push(obj1.top());
			objT.push(obj1.top());
			obj1.pop();
		}

		while(!objT.isLinkedEmpty())
		{
			obj1.push(top());
			objT.pop();
		}
	}

}
Last edited on
At line 9 of the copy constructor, you're going beyond the bounds of the array.
 
	for(int i=0; i<=st; i++)


Should be:
 
	for(int i=0; i<st; i++)


Also, line 7 is unnecessary. if st is 0, the for loop won't execute (assuming < is the terminating condition).






Topic archived. No new replies allowed.