C++ method that lets a function return an array

  • 2020-10-23 20:11:42
  • OfStack

This is a very elementary question, but it can be a headache for the beginner. In C++, a function cannot directly return an array, but an array is actually a pointer, so it can be used to return a pointer. For example, a function multiplied by a matrix could easily be written as:


#include <iostream>

using namespace std;

float* MultMatrix(float A[4], float B[4])
{
  float M[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];

  return M;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[4] = {1, 1, 0, 0};
  float *M = MultMatrix(A, B);
  cout << M[0] << " " << M[1] << endl;
  cout << M[2] << " " << M[3] << endl;

  return 0;
}

However, after running, the results are as follows:

[

1.75 1.75
6.51468e-039 3.76489e-039

]

Ben is not the desired result. So we add the display code in 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 not the same as the last result. Why is that?

Because the array M defined in the function is released by the system after the function is executed, the result obtained in the calling function is certainly not the calculated result. One solution is to dynamically allocate memory, new 1 array in the function, so that it is not freed.

Then you should put:


float M[4];

To:


float *M = new float[4];

Results obtained after modification:

[

1.75 1.75
0 0
1.75 1.75
0 0

]

Correct. But we're not freeing the space that we're claiming, and if we were to free it in the function it would look exactly like what we started with.

Take a look at our call code:


float *M = MultMatrix(A, B);

In this way, M pointer is pointed to the first address of the M array in the function. We can release the M pointer. The effect and the requested M array are the same, because they point to the same slice of memory space. So the code is changed to:


#include <iostream>

using namespace std;

float* MultMatrix(float A[4], float B[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;

  return M;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[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 results:

[

1.75 1.75
0 0
1.75 1.75
0 0

]

No problem, new space is also delete lost.

In view of the following Suggestions, I will modify the procedure as follows, please see if you can:


#include <iostream>

using namespace std;

void MultMatrix(float M[4], float A[4], float B[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;
}

int main()
{
  float A[4] = { 1.75, 0.66, 0, 1.75 };
  float B[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;
}
[

Comments:

First, delete of the array is delete[].

Second, one of the important principles of manual memory allocation in C++ is who allocates and who releases.

Therefore, the new array should not be in MultMatrix, but should be passed in after the new is finished.

To return an array, use something like a smart pointer.

]

That's how C++ lets a function return an array. For more information on C++ lets a function return an array, check out other articles on this site!


Related articles: