Problem with a string of integers

Hi everybody! I have a little problem with a string of integers.
After assignment 2d array from stack to 2d array in dynamical memory becomes a problem.
2d array in stack {{1,89,12,2,44},{23,5,32,16,18},{22,5,7,45,67}}
2d array in dyn.m {{1,89,12,2,23},{23,5,32,16,22},{22,5,7,45,67}}
Here is a code:
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
#include <iostream>

#define COL 3
#define ROW 5

using namespace std;

int** Array(int arr[][ROW])
{
	int **pd;
	pd=new int*[ROW];
	for(int i=0; i<ROW; i++)
		pd[i]=new int[COL];

	for(int i=0; i<COL; i++)
	{
		for(int j=0; j<ROW; j++)
		{
			pd[i][j]=arr[i][j];   //In this line a problem
		}
	}
	for(int i=0; i<COL; i++)
	{
		for(int j=0; j<ROW; j++)
		{
			cout<<arr[i][j]<<" ";
		}
	}cout<<endl;
	for(int i=0; i<COL; i++)
	{
		for(int j=0; j<ROW; j++)
		{
			cout<<pd[i][j]<<" ";
		}
	}cout<<endl;
	return pd;
}

void free(int **p)
{
	for(int i=0; i<ROW; i++)
		delete [] p[i];
	delete [] p;
}

int main()
{
	int arr[COL][ROW]={{1,89,12,2,44},{23,5,32,16,18},{22,5,7,45,67}};
	int **p;
	
	p=Array(arr);
	for(int i=0; i<COL; i++)
	{
		for(int j=0; j<ROW; j++)
		{
			cout<<p[i][j]<<" ";
		}
		cout<<endl;
	}
	//free(p);
	
	return 0;
}
the problem is not on lne 19. it's on line 11/13. you mixed up ROW and COL

First COL then ROW

EDIT: you mixed that up on line 48 again. It doesn't crash because the initialization doesn't go out of bounds
Last edited on
coder777, thank you very much. I did so stupid mistake.
Topic archived. No new replies allowed.