This is a very basic question, but it may be a headache for beginners who don't know about it. In C++, a function cannot directly return an array, but an array is essentially a pointer, so you can have the function return a pointer to achieve this. For example, for a matrix multiplication function, we can easily write:
Example
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;
}
But after running it, we find the result is:
1.75 1.75 6.51468e-039 3.76489e-039
This is not the desired result. So we add display code inside the function to see if it's a calculation problem, and we get the result:
1.75 1.75 0 0 1.75 1.75 1.96875 1.75
We find that the calculation result is correct, but it changes after being returned, and it's different from the previous result. Why is this?
Because the array M defined in the function is released by the system after the function finishes executing, so the result obtained when calling the function is naturally not the calculated result. One solution is to dynamically allocate memory, using new to create an array inside the function, so that it won't be released.
So we should change:
float M[4];
to:
float *M = new float[4];
After the modification, running it gives the result:
1.75 1.75 0 0 1.75 1.75 0 0
Correct. However, we haven't released the space we allocated ourselves. If we release it inside the function, the result will be the same as it was at the beginning.
Look at our calling code:
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, and the effect is the same as releasing the allocated M array, because they point to the same memory space. So the code is modified to:
Example
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;
}
Running result:
1.75 1.75 0 0 1.75 1.75 0 0
No problem, the space allocated with new is also deleted.
Given the suggestions from the experts below, I have modified the program as follows. Everyone, see if it's acceptable:
Example
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;
}
Review comments:
First, the delete for an array should be delete[].
Second, an important principle of manual memory allocation in C++ is that whoever allocates the memory should release it.
Therefore, you should not use new to create an array inside MultMatrix; instead, you should allocate it outside and pass it in for modification.
To return an array, using something like smart pointers is the right way.
Original address: https://www.cnblogs.com/yangxi/archive/2011/09/18/2180759.html