two demension arrauy

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
Hi
I want to extend a two demension array in every funcion call. THis what ive got so far:

#include <iostream>
#include <conio.h>
using namespace std;


char ** odbiera;
char **przechowuje;
 int ile =1;

void  odb( char *txt )


{
	
	if(!przechowuje)
	{
		
		przechowuje =new char*[ile];

		for(int i=0;i<ile;i++)
			przechowuje[i]=new char[100];

		strcpy (przechowuje[0], txt );
	ile++;
	}


	

	else {   
		
		if(odbiera)
			delete []odbiera;

		odbiera =new char*[ile];
		for(int i=0;i<ile;i++)
			odbiera[i]=new char[100];

		for(int i=0; i<ile-1; i++)
		{


			strcpy (odbiera[i], przechowuje[i] );
		}
		strcpy (odbiera[ile-1], txt );

		if(przechowuje)
			delete [] przechowuje;
		przechowuje = new char*[ile];

		for(int i=0;i<ile;i++)
			przechowuje[i]=new char[100];


		for(int i=0; i<ile-1; i++)
		{


			strcpy (przechowuje[i], odbiera[i] );
		}





	
	ile++;
	}
	

}



int main()

{

	odb("tekst");

	odb("rak");
	
	odb("co");

	for (int i=0;i<3;i++)
		cout<<przechowuje[i];

	getch();

	delete [] przechowuje;
	delete[] odbiera;

	return 1;


}



Last edited on
Firstly, http://www.cplusplus.com/forum/articles/17108/
Secondly, leave the memory reallocation to strings and vectors. They'll do it better than you.

I think the key problem in your code is that your pointers are not initialized to 0, so the program thinks they point to something. (I assume delete []odbiera; breaks the program).

It seems to me that your algorithm is very inefficient. It could be
1
2
3
4
5
6
7
odbiera = new char*[ile];
for(int i = 0; i < ile-1; i++) odbiera[i] = przechowuje[i];
odbiera[ile-1] = new char[100];
strcpy(odbiera[ile-1], txt);
delete[] przechowuje;
przechowuje = odbiera;
odbiera = 0;
Thanks alot! Problem fixed and works prefectly
Topic archived. No new replies allowed.