This is a very elementary problem, but it may be a headache for those who don't know. A function in C + + cannot return an array directly, but an array is actually a pointer, so it can be implemented by a function that returns a pointer. such as a function of matrix multiplication, it is easy for us to write:
#include <iostream>using namespacestd;float* Multmatrix (floata[4],floatb[4]){ floatm[4]; m[0] = a[0]*b[0] + a[1]*b[2]; m[1] = a[0]*b[1] + a[1]*b[3]; m[2] = a[2]*b[0] + a[3]*b[2]; m[3] = a[2]*b[1] + a[3]*b[3]; returnM;}intMain () {floata[4] = {1.75,0.66,0,1.75 }; floatb[4] = {1,1,0,0}; float*m =Multmatrix (A, B); cout<< m[0] <<" "<< m[1] <<Endl; cout<< m[2] <<" "<< m[3] <<Endl; return 0;}
But after the run, the results are: 1.75 1.75
6.51468e-039 3.76489e-039
It's not the result you want. So we also add the display code to the function to see if it is a calculation problem and get the result:
1.75 1.75
0 0
1.75 1.75
1.96875 1.75
The result of the calculation was found to be correct, but it changed after it was returned, and it was not the same as the previous result. What is this for?
Since the array m defined in the function has been freed by the system after the function has been executed, the result obtained in the calling function is certainly not the result of the calculation. One solution is to allocate memory dynamically, and new an array in the function so that it will not be freed.
So it should be
7 float m[4];
Switch
7 Float *m = new Float[4];
The results are obtained after the modification is run:
1.75 1.75
0 0
1.75 1.75
0 0
That's right. But we do not release the space we have applied for, and if we release it within the function, the result will be the same as the beginning.
Take a look at our calling code:
float *m = Multmatrix (A, B);
This actually refers to the M pointer to the first address of the M array in the function, we can release the M pointer, the effect and the M array of the release request are the same, because they point to the same piece of memory space. The code is then modified to:
#include <iostream>using namespacestd;float* Multmatrix (floata[4],floatb[4]){ float*m =New float[4]; m[0] = a[0]*b[0] + a[1]*b[2]; m[1] = a[0]*b[1] + a[1]*b[3]; m[2] = a[2]*b[0] + a[3]*b[2]; m[3] = a[2]*b[1] + a[3]*b[3]; cout<< m[0] <<" "<< m[1] <<Endl; cout<< m[2] <<" "<< m[3] <<Endl; returnM;}intMain () {floata[4] = {1.75,0.66,0,1.75 }; floatb[4] = {1,1,0,0}; float*m =Multmatrix (A, B); cout<< m[0] <<" "<< m[1] <<Endl; cout<< m[2] <<" "<< m[3] <<Endl; Delete[] M; return 0;}
Operation Result:
1.75 1.75
0 0
1.75 1.75
0 0
No problem, New's space is also deleted.
Of course, it's best to apply the memory outside and then pass it to the function:
#include <iostream>using namespacestd;voidMultmatrix (floatm[4],floata[4],floatb[4]) {m[0] = a[0]*b[0] + a[1]*b[2]; m[1] = a[0]*b[1] + a[1]*b[3]; m[2] = a[2]*b[0] + a[3]*b[2]; m[3] = a[2]*b[1] + a[3]*b[3]; cout<< m[0] <<" "<< m[1] <<Endl; cout<< m[2] <<" "<< m[3] <<Endl;}intMain () {floata[4] = {1.75,0.66,0,1.75 }; floatb[4] = {1,1,0,0}; float*m =New float[4]; Multmatrix (M, A, B); cout<< m[0] <<" "<< m[1] <<Endl; cout<< m[2] <<" "<< m[3] <<Endl; Delete[] M; return 0;}
function returns an array