The C + + function returns a two-dimensional array_ C + + return array

Posted by persia on Sun, 23 Jan 2022 13:09:24 +0100

In C + +, a function cannot directly return an array, but the array is actually a pointer, so you can let the function return a pointer. For example, a matrix multiplication function can be easily written as:

 1 #include 
 2 
 3 using namespace std;
 4 
 5 float* MultMatrix(float A[4], float B[4])
 6 {
 7     float M[4];
 8     M[0] = A[0]*B[0] + A[1]*B[2];
 9     M[1] = A[0]*B[1] + A[1]*B[3];
10     M[2] = A[2]*B[0] + A[3]*B[2];
11     M[3] = A[2]*B[1] + A[3]*B[3];
12 
13     return M;
14 }
15 
16 int main()
17 {
18     float A[4] = { 1.75, 0.66, 0, 1.75 };
19     float B[4] = {1, 1, 0, 0};
20     float *M = MultMatrix(A, B);
21     cout <0] <" " <1] <endl;
22     cout <2] <" " <3] <endl;
23 
24     return 0;
25 }

However, it was found that the result was 1.75

 6.51468e-039 3.76489e-039

It's not the desired result at all. 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

It is found that the calculated result is correct, but it changes after returning, and it is different from the last result. Why?

Because the array M defined in the function has been released by the system after the function is executed, the result obtained in calling the function is certainly not the result after calculation. One solution is to dynamically allocate memory and create a new array in the function so that it will not be released.

So we should

7    float M[4];

Replace with:

7  float *M = new float[4];

Results obtained after modified operation:

1.75 1.75
0 0
1.75 1.75
0 0

correct. But we didn't release the space we applied for. If we release it in the function, the result will be the same as at the beginning.

Look at our calling code:

20    float *M = MultMatrix(A, B);

This actually points the M pointer to the first address of the M array in the function. We can release the M pointer. The effect is the same as that of the M array applied for release, because they point to the same memory space. So the code is modified to:

 1 #include 
 2 
 3 using namespace std;
 4 
 5 float* MultMatrix(float A[4], float B[4])
 6 {
 7     float *M = new float[4];
 8     M[0] = A[0]*B[0] + A[1]*B[2];
 9     M[1] = A[0]*B[1] + A[1]*B[3];
10     M[2] = A[2]*B[0] + A[3]*B[2];
11     M[3] = A[2]*B[1] + A[3]*B[3];
12     cout <0] <" " <1] <endl;
13     cout <2] <" " <3] <endl;
14 
15     return M;
16 }
17 
18 int main()
19 {
20     float A[4] = { 1.75, 0.66, 0, 1.75 };
21     float B[4] = {1, 1, 0, 0};
22     float *M = MultMatrix(A, B);
23     cout <0] <" " <1] <endl;
24     cout <2] <" " <3] <endl;
25     delete[] M;
26 
27     return 0;
28 }

Operation results:

1.75 1.75
0 0
1.75 1.75
0 0


No problem. The new space is also delete d.

In view of the following suggestions, I will modify the procedure as follows. Can you see:

 1 #include 
 2 
 3 using namespace std;
 4 
 5 void MultMatrix(float M[4], float A[4], float B[4])
 6 {
 7     M[0] = A[0]*B[0] + A[1]*B[2];
 8     M[1] = A[0]*B[1] + A[1]*B[3];
 9     M[2] = A[2]*B[0] + A[3]*B[2];
10     M[3] = A[2]*B[1] + A[3]*B[3];
11 
12     cout <0] <" " <1] <endl;
13     cout <2] <" " <3] <endl;
14 }
15 
16 int main()
17 {
18     float A[4] = { 1.75, 0.66, 0, 1.75 };
19     float B[4] = {1, 1, 0, 0};
20 
21     float *M = new float[4];
22     MultMatrix(M, A, B);
23 
24     cout <0] <" " <1] <endl;
25     cout <2] <" " <3] <endl;
26     delete[] M;
27 
28     return 0;
29 }