CRT detected that the application wrote to memory after end of heap buffer

I would like to delete the pointer at the end of my program. So i wrote
delete ptr1; on line 78. But "CRT detected that the application wrote to memory after end of heap buffer" error popped out. Why is that and how can I solve it?


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  #include<iostream>
using namespace std;

void three(int **p,int rows,int cols,int rowe,int cole,bool &same);
int main(){
	int size,temp;
	bool run(0),same(0);
	cout<<"Please input the size of Sudoku [3, 9]: ";
	while(cin>>size){
		if(size==3||size==9) { 
		run=1;
		break;
		}
		
		else cout<<"Invalid input ! Please input either 3 or 9 ! " ;
	}

	if(run==1){
		int **ptr1=new int *[size]; 
	    for(int i=0;i<=size;++i)
			ptr1[i]= new int[size];

		cout<<"You are check " <<size<<"x"<<size;
		if(size==3)
			cout<<" sub-Sudoku, please input the numbers: ";
		else 
			cout<<" Sudoku, please input the numbers: ";

	    for(int i=0;i<size;i++){
			for(int y=0;y<size;y++){
			cin>>ptr1[i][y];
			}
		}
		
		if(size==3)
			three(ptr1,0,0,3,3,same);

		if(size==9){
			three(ptr1,0,0,3,3,same);
			three(ptr1,0,3,3,6,same);
			three(ptr1,0,6,3,9,same);
			three(ptr1,3,0,6,3,same);
			three(ptr1,3,3,6,6,same);
			three(ptr1,3,6,6,9,same);
			three(ptr1,6,0,9,3,same);
			three(ptr1,6,3,9,6,same);
			three(ptr1,6,6,9,9,same);

        //check if rows contains duplication
        for(int i=0;i<9;i++){
			temp=ptr1[i][0];
			for(int j=1;j<9;j++){
            if(temp==ptr1[i][j])same=1;
			}
		}
		//check if cols contains duplication
		for(int i=0;i<9;i++){
			temp=ptr1[0][i];
			for(int j=1;j<9;j++){
            if(temp==ptr1[j][i])same=1;
			}
		}
		}

		if(same){
			if(size==3)
			cout<<"It is an invalid sub-Sudoku!\n";
			else
			cout<<"It is an invalid Sudoku!\n";
		}
		else {
			if(size==3)
			cout<<"It is an valid sub-Sudoku!\n";
			else 
			cout<<"It is an valid Sudoku!\n";
		}

		delete ptr1;
		}

	

	return 0;
}

//rows=row start, rowe=row end
//check if 3x3 contains duplications
void three(int **p,int rows,int cols,int rowe,int cole,bool &same){
	//extract 2d array into 1d array
	int array[9],temp,j(0);
	for(int i=rows;i<rowe;i++){
			for(int y=cols;y<cole;y++){
				array[j]=p[i][y];
				j++;
	     }
	}
	//check if duplications exist
	for(int i=0;i<9;i++){
    temp=array[i];
	for(int j=0;j<9;j++){
		if(temp==array[j] && i!=j)
			same=1;
	}
}
}
Last edited on
The error actually happens at a very early stage.
Checkout line 20, you allocated memory for ptr[0] ~ ptr[size]. There're size + 1 allocations, while you have only size + 0 slots in the array.
Last edited on
But there's no errors when I haven't included the 'delete'. So I guess there is no problems on line 20.
I changed line 20 and it really works now, thanks for your kind help!
Topic archived. No new replies allowed.