I’m working on a Matrix class, I don’t understand why I’m getting this warning on this operator :
class Matrice {
int m, n;
double* tab ;
int* compteur;
public:
Matrice();
Matrice(int nlin, int ncol);
Matrice(int size);
~Matrice();
Matrice(const Matrice& A);
int nbLine();
int nbCol();
double get(int m, int n);
void set(int m, int n, double x);
void printImagineMatrix();
void printMatrix();
Matrice operator+(Matrice B);
Matrice operator*(double x);
Matrice operator*(Matrice A);
void operator=(const Matrice& B);
};
void Matrice::operator=(const Matrice& B){
delete [] tab;
m = B.m;
n = B.n;
tab = new double[m*n];
double *a = tab, *b = B.tab;
for(int i=0; i<m*n; i++){
*a++ = *b++; // HERE I get : Assigned value is garbage or undefined [clang-analyzer core.uninitialized.Assign]
}
}
I don’t know how to fix this problem.
Thank you for your help.
This question’s shown code fails to meet Stackoverflow’s requirements for showing a minimal reproducible example. Because of that it’s unlikely that anyone here can conclusively answer the question; but only guess at the most. You need to edit your question to show a minimal example, no more than one or two pages of code (the “minimal” part), that everyone else can cut/paste exactly as shown, compile, run, and reproduce the described issue (the “reproducible” part, this includes any ancillary information, like any input to the program). See How to Ask for more information.
Not an answer but I would advice you to declare multiple variables on multiple lines. Especially with pointers. I would write
double *a = tab, *b = B.tab;
asdouble* a = tab;
anddouble* b = B.tab;
(and probably use brace initialization).Write the copy constructor and destructor first. Then when you get those coded,
operator=
is a very simple operation consisting of just a fewstd::swap
calls. Also, if you have not written the copy constructor and destructor, you really can’t reliably test your code. You need to implement all 3 functions fully before you attempt to testMatrice
correctly.In addition to @PaulMcKenzie’s comment have a look at the copy swap idiom.
The error is telling you that
B.tab
does not point to initialized data. None of the code that you show indicates thatB.tab
does point to initialized data. Although there seems to be a lot of code that we can’t see.Show 4 more comments