Try to make larger array (dynamic array)

I try to make "m" larger during the program is ran...
I try few things, but nothing works...
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
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::nothrow;
    using std::cerr;
    
    int main()
    {
    	int n=3;
    	int **m;
    	//The initial array:
    	m=new (nothrow) int *[n];
    	for (int i=0;i<n;i++)
    	{
    		m[i]=new (nothrow) int[3];
    		m[i][0]=i+1;
    		m[i][1]=i+2;
    		m[i][2]=i+3;
    	}
    	for (int i=0;i<n;i++)
    				cout<<m[i][0]<<" "<<m[i][1]<<" "<<m[i][2]<<endl;
    	cout<<"----------------------------------------------\n";
    	int **t; //The temporary array:
    	t=new (nothrow) int *[n*2];
    	for (int i=0; i<n; i++)
    	{
    		t[i]=new int[3];
    		t[i+n]=new int[3];
    		t[i]=m[i];  //#####//
    		cout<<t[i][0]<<" @ "<<t[i][1]<<" @ "<<t[i][2]<<endl;
    	}
    	cout<<"\n\n\n";
    	for (int i=n; i<n*2; i++)
    	{
    		for (int j=0; j<n; j++)
    			t[i][j]=j+i;
    		cout<<t[i][0]<<" % "<<t[i][1]<<" % "<<t[i][2]<<endl;
    	}
    	cout<<"\n\n\n";
    	for (int i=0;i<n;i++)
    		delete []m[i];
    	delete []m;
    		m=t;  //#####//
    	
    	//Printing the final array:
    	for (int i=0;i<n*2;i++)
    		cout<<m[i][0]<<" "<<m[i][1]<<" "<<m[i][2]<<endl;
    
    	for (int i=0;i<n;i++)
    			delete []t[i];
    		delete []t;
    		return 0;
    }

Why I didn't get the array ?

here is the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1 2 3
2 3 4
3 4 5
1 @ 2 @ 3
2 @ 3 @ 4
3 @ 4 @ 5



3 % 4 % 5
4 % 5 % 6
5 % 6 % 7



0 2 3
156127248 3 4
156127264 4 5
3 4 5
4 5 6
5 6 7


Can you find my error??
(look at the bold lines at the output...)

Thank you!!
Last edited on
Someone??
Please...
I really need your help!!!
closed account (D80DSL3A)
Your error is at line 31, which changes the assignments made on line 29.
If I delete line 31, the first "m" array didn't copy:

This is what I got:
0 0 0
0 0 0
0 0 0
3 4 5
4 5 6
5 6 7

There should be:
1 2 3
2 3 4
3 4 5
(instead of the zeros).

Thank you!
closed account (D80DSL3A)
Don't delete line 31. I thought you would realize the error once it was pointed out which line was wrong. Didn't you mean to copy the values stored in m[i][0], m[i][1] and m[i][2] instead?
t[i][0] = m[i][0]; t[i][1] = m[i][1]; t[i][2] = m[i][2];
Last edited on
Yes, this is what I have to do...
line 31 don't do this?

Thank you!
closed account (D80DSL3A)
You're welcome.
line 31 don't do this?

No. It does the same thing line 29 does. It assigns the pointers to point to some area of memory. This: t[i] = m[i]; makes the t[i] point to memory which is deleted on line 43.
Last edited on
THANK YOU!!

so if I'm understanding you right,
I have to copy each item at the specific line?
e.g.:
1
2
for (int i=0; i<line_len; i++)
     t[3][i]=m[3][i];

(3 - is a specific line, it can be any other line...)

I'm right, yes?

Thank you again :-)
closed account (D80DSL3A)
I think if you replace line 31 with:
t[i][0] = m[i][0]; t[i][1] = m[i][1]; t[i][2] = m[i][2];
your troubles will be gone.
Thank you so much fun2code!!!
It's solve my problem!!! :-)
Topic archived. No new replies allowed.