Dynamic 2D array allocation help

Hello cpp!

I have a problem, I get undesired output.

My program simply ask the user for the 2D array indices and then COUT them.

For some reason, the output always i = 0 and j = 0.

Why is that?

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
#include <iostream>

using namespace std;

void bar(int **&arr, int &firstIndex, int &secondIndex)
{
	//dynamic array allocation
	arr = new int *[firstIndex];
	for (int i = 0; i < firstIndex; ++i)
	{
		arr[i] = new int[secondIndex];
	}

	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			arr[i][j] = i*j;
		}
	}
}

int main()
{
	//asking user for info
	int firstIndex, secondIndex;
	cout << "Please enter two integers, first = [x], second = [y] indices: ";
	cin >> firstIndex >> secondIndex;

	//allocating a dynamic array in main()
	int **arr = new int*[firstIndex];
	for (int i = 0; i < firstIndex; ++i)
	{
		arr[i] = new int[secondIndex];
	}

	//calling bar()
	bar(arr, firstIndex, secondIndex);

	//output to screen
	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			cout << "i = " << i << "  j = " << j << "y[i][j] = " << arr[i][j] << endl;
		}
	}

	//deallocating dynamic array
	delete[]arr;

	system("Pause");
	return 0;
}
Last edited on
It works for me.

Why do you create the array twice? I'd say either on line 8 or 31. This way you have a memory leak
I fixed it, still getting undesired output. The function doesn't work properly for some reason.. :/

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
#include <iostream>

using namespace std;

void bar(int **&arr, int &firstIndex, int &secondIndex)
{
	//dynamic array allocation
	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			arr[i][j] = i*j;
		}
	}
}

int main()
{
	//asking user for info
	int firstIndex, secondIndex;
	cout << "Please enter two integers, first = [x], second = [y] indices: ";
	cin >> firstIndex >> secondIndex;

	//allocating a dynamic array in main()
	int **arr = new int*[firstIndex];
	for (int i = 0; i < firstIndex; ++i)
	{
		arr[i] = new int[secondIndex];
	}

	//calling bar()
	bar(arr, firstIndex, secondIndex);

	//output to screen
	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			cout << "i = " << i << " j = " << j << " y[i][j] = " << arr[i][j] << endl;
		}
	}

	//deallocating dynamic array
	delete[]arr;

	system("Pause");
	return 0;
}
My output is this:
Please enter two integers, first = [x], second = [y] indices: 2 3
i = 0 j = 0 y[i][j] = 0
i = 0 j = 1 y[i][j] = 0
i = 0 j = 2 y[i][j] = 0
i = 1 j = 0 y[i][j] = 0
i = 1 j = 1 y[i][j] = 1
i = 1 j = 2 y[i][j] = 2
your delete[] process is incorrect for 2D array!
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
// 2D dynamic array
#include <iostream>
using namespace std;

void setVal(int **ar, const int r, const int c)
{
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
			ar[i][j] = i+j;
	}
return;
}


int main()
{
	int row=3, column=5;
	//cout << "enter row and column number:\n";
	//cin >> row >> column;

	int **arr = new int*[row];
	for (int i = 0; i < row; ++i)
	{
		arr[i] = new int[column];
	}

	setVal(arr, row, column);

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			cout << "arr[" << i << "][" << j << "]= " << arr[i][j] << "  ";
		}
		cout << "\n";
	}

	for(int i=0; i<row; i++)
		delete[] arr[i];

	delete[] arr;

return 0;
}
Why did you comment the cout and cin? I want my program to work regarding what the user inputs. That's why I am using Dynamic 2D array. Or is that not possible?
@DeathLeap, anup30 was just testing the code with variables without getting them from user input, see int row=3 and column=5. You can take that part out and still prompt the user for their input.
absolutely possible!
i disabled them, to code faster. ( its faster with default values/without user input to debug/ fix error )

and in your 5th line void bar(int **&arr,
you don't need &, because **arr is already address.

when deleting 2D array, remember to delete[] what is allocated in line11 arr[i] = new int[secondIndex]; first. otherwise memory leak will occur.
Okay.

I got two questions:

Look at my code, is it perfect now? if not, how can I make it better?

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
#include <iostream>

using namespace std;

void bar(int **arr, int &firstIndex, int &secondIndex)
{
	//dynamic array allocation
	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			arr[i][j] = i*j;
		}
	}
}

int main()
{
	//asking user for info
	int firstIndex, secondIndex;
	cout << "Please enter two integers, first = [x], second = [y] indices: ";
	cin >> firstIndex >> secondIndex;

	//allocating a dynamic array in main()
	int **arr = new int*[firstIndex];
	for (int i = 0; i < firstIndex; ++i)
	{
		arr[i] = new int[secondIndex];
	}

	//calling bar()
	bar(arr, firstIndex, secondIndex);

	//output to screen
	for (int i = 0; i < firstIndex; i++)
	{
		for (int j = 0; j < secondIndex; j++)
		{
			cout << "i = " << i << " j = " << j << " y[i][j] = " << arr[i][j] << endl;
		}
	}

	//deallocating dynamic array
	delete[]arr;

	system("Pause");
	return 0;
}


My second question is, what happens if a "memory leak" occurs?
if you do with normal 2D array,

1
2
3
4
int row, column;
cin >> row >> column;

int arr[row][column];
its possible in gcc compiler. but visual studio complains about it.

or, you can use vector, you need not worry about deleting and can resize rows and columns easily. (resizing in dynamic 2d array is harder)
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
// 2D vector
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int row, column;
	cin >> row >> column;
	
	vector<int> ar;  // linear vector
	vector<vector<int> > arr; // 2D vector

	for (int i=0; i<row; i++)
	{
		arr.push_back(ar);
	}
	
	
	for (int i=0; i<row; i++)
	{
		for(int j=0; j<column; j++)
		{
			arr[i].push_back(i+j);
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}	
		
return 0;
}


is it perfect now?
your delete process is still wrong.
you need
1
2
3
4
5
for(int i=0; i<row; i++)
{
  delete[] arr[i];
}
delete[] arr;


My second question is, what happens if a "memory leak" occurs?

in your program, actually there wont be any problem if you don't delete. OS will automatically delete after program exit.

but in large/long running program, when you don't need previously allocated memory anymore, you have to say the computer that previously called memory to De-allocate, by delete/delete[]
so the computer(OS) can use the memory for another program or you can use that memory for another task later. otherwise, it will quickly reach out of ram's capacity.

so its good practice to know/use them from beginning.


EDIT: and you can hang computer by memory leaking!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//  !!!!!! program to hang Computer !!!!!!!
#include <iostream>
using namespace std;

int main()
{
	unsigned long long int *pt= new unsigned long long int;
	unsigned long long int size = 4e9; // 4e9 == 4 billion, requires approx 16GB Ram.
	unsigned long long int i;
	
	for(i=0; i<size; i++) 
	{
		//delete pt; //disabled to leak memory & hang pc
		pt = new unsigned long long int;
	}
		
return 0;
}

if your PC has more than 16GB Ram, increase the size in line8 or use infinite loop!!
Last edited on
Topic archived. No new replies allowed.