你好我想通过我的所有的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