Bonsoir,

Je suis en train de fignoler un projet de C++ qui utilise une classe Matrix.
J'ai un problème lorsque je teste les différentes méthodes de ma classes, il bloque sur la méthode "create_empty_matrix(unsigned int, unsigned int)"

Mais je ne voit pas du tout pourquoi il veut pas... Lors du debug, il parle de malloc, mais je ne voit pas ou j'aurais des problèmes de mémoire...


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef MATRIX_H
#define MATRIX_H
 
#include "stdafx.h"
#include <iostream>
 
class Matrix
{
public: //________________________________________________________
 
	// CONSTRUCTOR
	Matrix();
	Matrix(unsigned int, unsigned int,double);
	Matrix(double);
	Matrix(const Matrix&);
	virtual ~Matrix();
 
	// GET/SET
 
	unsigned int get_i() const;
	void set_i(unsigned int);
	void set_i();
 
	unsigned int get_j() const;
	void set_j(unsigned int);
	void set_j();
 
	double get_matrix_element(unsigned int, unsigned int) const;
	void set_matrix_element(unsigned int, unsigned int, double);
	void set_matrix_element();
 
	double** get_matrix() const;
	void set_matrix(const Matrix&);
 
	// METHOD
 
	void fill_matrix();
	void define_matrix();
 
	void create_empty_matrix(unsigned int, unsigned int);
 
	friend Matrix& operator + (const Matrix&, const Matrix&);
	friend Matrix& operator - (const Matrix&, const Matrix&);
	friend Matrix& operator * (const Matrix&, const Matrix&);
	Matrix& operator = (const Matrix&);
 
	void view_matrix() const;
 
protected: //_____________________________________________________
private: //_______________________________________________________
 
	unsigned int i; //Nombre de ligne
	unsigned int j; //Nombre de colonne
	double** matrix;
 
};
 
void testMatrix();
 
#endif MATRIX_H
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "stdafx.h"
#include "Matrix.h"
#include <string>
 
//### PUBLIC ####################################################################
//###############################################################################
 
//_______________________________________________________________________________
// CONSTRUCTOR
 
Matrix::Matrix():
	i(1),j(1)
{
	double** buffer = new double*[i];
	for(unsigned int t=0; t<i; t++)
	{
		buffer [t] = new double[j];
	}
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			buffer[t][p] = 0;
		}
	}
	matrix = buffer;
}
 
Matrix::Matrix(unsigned int new_i, unsigned int new_j,double value):
	i(new_i), j(new_j)
{
	double** buffer = new double*[i];
	for(unsigned int t=0; t<i;t++)
	{
		buffer[t] = new double[j];
	}
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			buffer[t][p] = value;
		}
	}
	matrix = buffer;
}
 
Matrix::Matrix(double value):
	i(1),j(1)
{
	double** buffer = new double*[i];
	for(unsigned int t=0; t<i;t++)
	{
		buffer[t] = new double[j];
	}
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			buffer[t][p] = value;
		}
	}
	matrix = buffer;
}
 
Matrix::Matrix(const Matrix& CopyMatrix):
	i(CopyMatrix.get_i()), j(CopyMatrix.get_j())
{
	matrix = new double*[i];
	for(unsigned int t=0; t<i;t++)
	{
		matrix[t] = new double[j];
	}
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			matrix[t][p] = CopyMatrix.get_matrix_element(t,p);
		}
	}
}
 
Matrix::~Matrix()
{
	if (matrix != NULL){
		for(unsigned int t=0; t<i; t++)
		{
			delete [] matrix[t];
		}
		delete [] matrix;
	}
}
 
//_______________________________________________________________________________
// GET/SET
 
unsigned int Matrix::get_i() const
{
	return i;
}
 
void Matrix::set_i(unsigned int new_i)
{
	if(new_i != i)
	{
		i = new_i;
		create_empty_matrix(i,j);
	}
}
 
void Matrix::set_i()
{
	unsigned int new_i;
	std::cin >> new_i;
	if(new_i != i)
	{
		i = new_i;
		create_empty_matrix(i,j);
	}
}
 
unsigned int Matrix::get_j() const
{
	return j;
}
 
void Matrix::set_j(unsigned int new_j)
{
	if(new_j != j)
	{
		j = new_j;
		create_empty_matrix(i,j);
	}
}
 
void Matrix::set_j()
{
	unsigned int new_j;
	std::cin >> new_j;
	if(new_j != j)
	{
		j = new_j;
		create_empty_matrix(i,j);
	}
}
 
double Matrix::get_matrix_element(unsigned int _i, unsigned int _j) const
{
	if(_i<i && _j<j)
	{
		return matrix[_i][_j];
	} else {
		throw std::string("Out of range ! Function: 'Matrix::get_matrix_element(unsigned int, unsigned in)'");
	}
}
 
void Matrix::set_matrix_element(unsigned int _i, unsigned int _j, double value)
{
	if(_i<i && _j<j)
	{
		matrix[_i][_j] = value;
	} else {
		throw std::string("Out of range ! Function: 'Matrix::set_matrix_element(unsigned int, unsigned int)'");
	}
}
 
void Matrix::set_matrix_element()
{
	unsigned int _i,_j;
	double value;
	std::cout << std::endl << "Entrez le numero de ligne (Commence a '0' !): ";
	std::cin >> _i;
	std::cout << std::endl << "Entrez le numero de colonne (Commence a '0' !): ";
	std::cin >> _j;
	std::cout << std::endl << "Entrez la valeur de l'element [" << _i << "],[" << _j << "] :";
	std::cin >> value;
	if(_i<i && _j<j)
	{
		matrix[_i][_j] = value;
	} else {
		throw std::out_of_range("Out of range ! Function: 'Matrix::set_matrix_element()'");
	}
}
 
double** Matrix::get_matrix() const
{
	double** buffer = new double*[i];
	for(unsigned int t=0; t<i; t++)
	{
		buffer [t] = new double[j];
	}
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			buffer[t][p] = matrix[t][p];
		}
	}
	return buffer;
}
 
void Matrix::set_matrix(const Matrix& CopyMatrix)
{
	i=CopyMatrix.get_i();
	j=CopyMatrix.get_j();
	create_empty_matrix(i,j);
	for(unsigned int t=0; t<i;t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			matrix[t][p] = CopyMatrix.get_matrix_element(t,p);
		}
	}
}
 
//_______________________________________________________________________________
// METHOD
 
void Matrix::fill_matrix()
{
	double value;
	std::cout << std::endl << "C'est une matrice " << i << " par " << j;
	view_matrix();
	for(unsigned int t=0; t<i; t++)
	{
		for(unsigned int p=0; p<j; p++)
		{
			std::cout << std::endl << "Entrez la valeur de la " << t << " ligne et de la " << p << " colonne (Commence a '0' !): ";
			std::cin >> value;
			set_matrix_element(t, p, value);
			std::cout << std::endl;
			view_matrix();
		}
	}
}
 
void Matrix::define_matrix()
{
	std::cout << std::endl << "Rentrer le nouveau nombre de ligne (Cette action effacera la matrice): ";
	set_i();
	std::cout << std::endl << "Rentrer le nouveau nombre de colonne (Cette action effacera la matrice): ";
	set_j();
	fill_matrix();
}
 
void Matrix::create_empty_matrix(unsigned int _i, unsigned int _j)
{
	double** buffer = new double*[_i];
	for(unsigned int t=0; t<_i; t++)
	{
		buffer [t] = new double[_j];
	}
	for(unsigned int t=0; t<_i; t++)
	{
		for(unsigned int p=0; p<_j; p++)
		{
			buffer[t][p] = 0;
		}
	}
 
	matrix = buffer;
}
 
Matrix& operator + (const Matrix& CopyMatrixA, const Matrix& CopyMatrixB)
{
	if(CopyMatrixA.get_i()==CopyMatrixB.get_i() && CopyMatrixA.get_j()==CopyMatrixB.get_j())
	{
		Matrix a(CopyMatrixA.get_i(),CopyMatrixA.get_j(),0);
		for(unsigned int t=0; t<CopyMatrixA.get_i(); t++)
		{
			for(unsigned int p=0; p<CopyMatrixA.get_j(); p++)
			{
				a.set_matrix_element(t,p,(CopyMatrixA.get_matrix_element(t,p) + CopyMatrixB.get_matrix_element(t,p)));
			}
		}
		return a;
	} else {
		throw std::string("Different matrix size ! Function: 'friend operator + (const Matrix&, const Matrix&)'");
	}
}
 
Matrix& operator - (const Matrix& CopyMatrixA, const Matrix& CopyMatrixB)
{		
	if(CopyMatrixA.get_i()==CopyMatrixB.get_i() && CopyMatrixA.get_j()==CopyMatrixB.get_j())
	{
		Matrix a(CopyMatrixA.get_i(),CopyMatrixA.get_j(),0);
		for(unsigned int t=0; t<CopyMatrixA.get_i(); t++)
		{	
			for(unsigned int p=0; p<CopyMatrixA.get_j(); p++)
			{
				a.set_matrix_element(t,p,CopyMatrixA.get_matrix_element(t,p) - CopyMatrixB.get_matrix_element(t,p));
			}
		}
		return a;
	} else {
		throw std::string("Different matrix size ! Function: 'friend operator - (const Matrix&, const Matrix&)'");
	}
}
 
Matrix& operator * (const Matrix& CopyMatrixA, const Matrix& CopyMatrixB)
{	
	if(CopyMatrixA.get_j()==CopyMatrixB.get_i())
	{
		Matrix a(CopyMatrixA.get_i(),CopyMatrixB.get_j(),0);
		for(unsigned int t=0; t<CopyMatrixA.get_i(); t++)
		{
			for(unsigned int p=0; p<CopyMatrixB.get_j(); p++)
			{
				double tmp_data =0;
				for(unsigned int m=0; m<CopyMatrixA.get_i(); m++)
				{
					tmp_data += CopyMatrixA.get_matrix_element(t,m) * CopyMatrixB.get_matrix_element(m,p);
				}
				a.set_matrix_element(t,p,tmp_data);
				tmp_data=0;
			}
		}
		return a;
	}
	else if(CopyMatrixA.get_i()==1 && CopyMatrixA.get_j()==1)
	{
		Matrix a(CopyMatrixB.get_i(),CopyMatrixB.get_j(),0);
		for(unsigned int t=0; t<CopyMatrixB.get_i(); t++)
		{
			for(unsigned int p=0; p<CopyMatrixB.get_j(); p++)
			{
				a.set_matrix_element(t, p, CopyMatrixB.get_matrix_element(t,p)*CopyMatrixA.get_matrix_element(0,0));
			}
		}
		return a;
	}
	else if(CopyMatrixB.get_i()==1 && CopyMatrixB.get_j()==1)
	{
		Matrix a(CopyMatrixA.get_i(),CopyMatrixA.get_j(),0);
		for(unsigned int t=0; t<CopyMatrixA.get_i(); t++)
		{
			for(unsigned int p=0; p<CopyMatrixA.get_j(); p++)
			{
				a.set_matrix_element(t, p, CopyMatrixA.get_matrix_element(t,p)*CopyMatrixB.get_matrix_element(0,0));
			}
		}
		return a;
	} else {
		throw std::string("Different matrix size ! Function: 'friend operator * (const Matrix&, const Matrix&)'");
	}
 
 
}
 
Matrix& Matrix::operator = (const Matrix& CopyMatrix)
{
	if(this != &CopyMatrix){
		i = CopyMatrix.get_i();
		j = CopyMatrix.get_j();
		create_empty_matrix(i,j);
		for(unsigned int t=0; t<i; t++)
		{
			for(unsigned int p=0; p<j; p++)
			{
				matrix[t][p] = CopyMatrix.get_matrix_element(t,p);
			}
		}
	}
	return *this;
}
 
void Matrix::view_matrix() const
{
	std::cout << std::endl;
	for(unsigned int t=0; t<i; t++)
	{
		std::cout << "| ";
		for(unsigned int p=0; p<j; p++)
		{
			std::cout << matrix[t][p] << " ";
		}
		std::cout << "|" << std::endl;
	}
}
 
//### PROTECTED #################################################################
//###############################################################################
 
//### PRIVATE ###################################################################
//###############################################################################
 
 
void testMatrix()
{	
	Matrix a(2,2,0);
	a.define_matrix();
	a.view_matrix();
 
	Matrix c;
	std::cout << "Entre la nouvelle valeur de i" << std::endl;
	c.set_i();
	std::cout << "Entre la nouvelle valeur de j" << std::endl;
	c.set_j();
	c.view_matrix();
 
	Matrix b(a);
	b.view_matrix();
	a.set_matrix_element(0,0,8);
	a.view_matrix();
	b.view_matrix();
 
	std::cout << "__________________________________";
 
	Matrix d;
	d.view_matrix();
	c=a+b;
	c.view_matrix();
	a.set_matrix_element(0,0,1);
	c= b*2;
	c.view_matrix();
	c=a-b;
	c.view_matrix();
}