你好我想通過我的所有的malloc調用(ANS後續的malloc檢查)移動到一個程序,它看起來像到neaten我的代碼功能:的malloc在C++
int Malloc2D(int n, int m, double** array_ptr) {
array_ptr = (double**) malloc(n * sizeof(double*));
if (array_ptr == NULL) {
std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
return -1;
}
for (int i = 0; i < n; i++) {
array_ptr[i] = (double*) malloc(m * sizeof(double));
if (array_ptr[i] == NULL) {
std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
return -1;
}
}
return 0;
}
但在主時( )我做類似的東西:
double** test;
if(Malloc2d(10, 20, test) == -1) return -1;
然後嘗試使用主數組我得到一個段錯誤?任何人有任何想法? 插孔
所以數組的兩個維度並傳回一個指向開始的指針會好嗎? – JMzance
您可以通過C++中的引用 –
沒錯,我們可以去參考。我選擇不這樣做。 – abelenky