我對C++非常陌生,來自Fortran 90+和MATLAB背景。我一直在玩C++中的指針,並且偶然發現了一些我無法解釋的東西。 Google和Stack Overflow的搜索沒有真正解釋。在C++中返回指向具有普通函數和void函數的動態分配數組的指針
這是一個隨機的例子,我想到了,只是從亂搞,試圖瞭解C++如何工作。
實質上,我想要做的是在函數內分配一個動態數組,填充數組,然後在調用函數中返回一個指向數組的指針。如果我使用void函數將指針返回給數組,它不起作用。如果我把它作爲一個正常的函數返回,它就可以工作。我們分別稱這些案例1和案例2。
我已經把我的意思的例子:
#include <iostream>
using namespace std;
// Prototypes
void populate(int rows, int cols, double* ptr_to_mat);
double* populate_return(const int rows, const int cols);
// Main function
int main()
{
int rows, cols;
cout << "Number of rows: ";
cin >> rows;
cout << "Number of columns: ";
cin >> cols;
cout << endl;
double* ptr_to_mat;
populate(rows, cols, ptr_to_mat);
cout << endl << "OUTPUT IN main FROM populate" << endl;
for (int r = 0; r <= rows-1; r++)
for (int c = 0; c <= cols-1; c++)
cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;
cout << endl << endl;
ptr_to_mat = populate_return(rows, cols);
cout << endl << "OUTPUT IN main FROM populate_return" << endl;
for (int r = 0; r <= rows-1; r++)
for (int c = 0; c <= cols-1; c++)
cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;
delete [] ptr_to_mat;
return 0;
}
// Other functions
// Return pointer by modifying argument -- CASE 1
void populate(const int rows, const int cols, double* ptr_to_mat)
{
double* internal_mat = new double[rows*cols];
cout << "INPUT IN populate" << endl;
for (int r = 0; r <= rows - 1; r++)
for (int c = 0; c <= cols - 1; c++)
{
cout << "mat(" << r << "," << c << ") = ";
cin >> internal_mat[c+cols*r];
}
ptr_to_mat = internal_mat;
cout << endl << "OUTPUT IN populate" << endl;
for (int r = 0; r <= rows-1; r++)
for (int c = 0; c <= cols-1; c++)
cout << "mat(" << r << "," << c << ") = " << ptr_to_mat[c+cols*r] << endl;
}
// Return pointer using "return" -- CASE 2
double* populate_return(const int rows, const int cols)
{
double* internal_mat = new double[rows*cols];
cout << "INPUT IN populate_return" << endl;
for (int r = 0; r <= rows - 1; r++)
for (int c = 0; c <= cols - 1; c++)
{
cout << "mat(" << r << "," << c << ") = ";
cin >> internal_mat[c+cols*r];
}
cout << endl << "OUTPUT IN populate_return" << endl;
for (int r = 0; r <= rows-1; r++)
for (int c = 0; c <= cols-1; c++)
cout << "mat(" << r << "," << c << ") = " << internal_mat[c+cols*r] << endl;
return internal_mat;
}
運行上面的代碼的結果如下:
Number of rows: 3
Number of columns: 2
CASE 1
INPUT IN populate
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6
OUTPUT IN populate
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6
OUTPUT IN main FROM populate
mat(0,0) = -1.72952e-41
mat(0,1) = -2.77962e-42
mat(1,0) = -2.77966e-42
mat(1,1) = -2.7797e-42
mat(2,0) = -6.02988e-42
mat(2,1) = -2.77979e-42
CASE 2
INPUT IN populate_return
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6
OUTPUT IN populate_return
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6
OUTPUT IN main FROM populate_return
mat(0,0) = 1
mat(0,1) = 2
mat(1,0) = 3
mat(1,1) = 4
mat(2,0) = 5
mat(2,1) = 6
我的假設是,它必須做當指針在調用函數中被分配時。因此,在情況2中,在將函數內的信息從內存釋放之前將其分配給左側,而在情況1中,在指針被分配到調用函數中之前釋放該內存,因爲分配發生在在右邊。
這聽起來似乎合理嗎?如果不是,案例2的工作原因是什麼,但案例1沒有?
如果我想在一個函數中分配多個動態數組並返回指向這些數組的指針,我必須使用void函數。在這種情況下可以做什麼/應該做什麼?
使用'的std ::矢量' –