tun time check failure?

Anything wrong with my code? run-time check failure #2:stack around the variable "ppointer" was currupted. What is the error about?

I could see the printout in the screen correctly though

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
34
35
  class practice
	{
	public:
		practice (){};
		practice (int a) 
		{variable1++;
	   variable2=a;}
		~practice () {variable1--;}

		int getvar(){return variable2;}
		static int variable1;
	private:
		
		int variable2;
	};

	int practice::variable1=0;

int main()
	{

	const int max=3;
	practice *PPointer[max];

	for (int i=1;i<=max;i++)
		{PPointer[i]=new practice(i);
	cout<<practice::variable1<<endl<<PPointer[i]->getvar()<<endl;}

	for (int j=1; j<=max;j++)
	{delete PPointer[j];
	PPointer[j]=0;
	cout<<practice::variable1<<endl;}

	return 0;
	}
You have problems with both for loops.

When you allocated your pointers at line 23, they were allocated from [0] to [2].
In both of your for loops, you indexing from [1] to [3].
[3] is out of range.

Your for loops should have been:
 
for (int j=0; j<max;j++)
Last edited on
Thanks!
Topic archived. No new replies allowed.