Delete array

Hello, the problem is that i do not know how to delete multi-dimensional array.
I tried smth like written below but it gives debug error.
CRT detected that the application wrote to memory after end of heap buffer.

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
#include <iostream>
using namespace std;

void InputArray(int** arr, int raw, int column);
void OutputArray(int** arr, int raw, int column);
void DelArr(int** arr, int raw, int column);

int main()
{
	int raw, column;

	cout << "Enter a number of raws: ";
	cin >> raw;
	cout << "Enter a number of columns: ";
	cin >> column;
	
	int** arr = new int*[raw];
	for (int i = 0; i <= raw; i++)
	{
		arr[i] = new int[column];
	}

	InputArray(arr, raw, column);
	OutputArray(arr, raw, column);
	DelArr(arr, raw, column);

    return 0;
}

void InputArray(int** arr, int raw, int column)
{
	for (int i = 0; i < raw; i++)
	{
		for (int j = 0; j < column; j++)
		{
			cin >> arr[i][j];
		}
	}
}

void OutputArray(int** arr, int raw, int column)
{
	for (int i = 0; i < raw; i++)
	{
		cout << endl;
		for (int j = 0; j < column; j++)
		{
			cout << arr[i][j]<<" ";
		}
	}
	cout << endl << endl;
}

void DelArr(int** arr, int raw, int column)
{
	for (int i = 0; i < raw; i++)
	{
		delete[] arr[i];
	}
	delete[] arr;
}
17
18
19
20
21
	int** arr = new int*[raw];
	for (int i = 0; i <= raw; i++)
	{
		arr[i] = new int[column];
	}


If i can be the same value as raw when the body of the loop is executed, then arr[i] will be memory you don't own but trample on anyway which results in undefined behavior.

for (int i=0; i<row; ++i)
Topic archived. No new replies allowed.