2013-07-02 58 views
0

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

然后尝试使用主数组我得到一个段错误?任何人有任何想法? 插孔

回答

1

C和C++是传递值。
因此,对功能Malloc2D内的array_ptr所做的更改在功能外部是不可见的。

你想传递一个指针来测试,并修改它的值。
(这会导致对三重指针;混淆,但不是无法控制的)

int Malloc2D(int n, int m, double*** pOutput) 
{ 
    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; 
     } 
    } 

    *pOutput = array_ptr; 
    return 0; 
} 

然后使用功能,合格test地址:

double** test; 
if(Malloc2d(10, 20, &test) == -1) return -1; 
+0

所以数组的两个维度并传回一个指向开始的指针会好吗? – JMzance

+0

您可以通过C++中的引用 –

+0

没错,我们可以去参考。我选择不这样做。 – abelenky

1

你应该通过一个指针或在您的double array_ptr的参考。在这里,它被复制,并且只有副本被修改。

int Malloc2D(int n, int m, double** &array_ptr) 
1

,然后尝试使用主数组我得到一个segfault

的问题是,因为它是被按值传递你没有修改array_ptr。一个可能的解决办法是通过引用传递它:

int Malloc2D(int n, int m, double** &array_ptr) 
            ^
2

既然你传递一个double **array_ptr它不会改变函数外部的指针。

在C++中,你可以通过使参考修复它(和使用new,因为它是C++)

int Malloc2D(int n, int m, double**& array_ptr) { 

array_ptr = new double*[n]); 
if (array_ptr == NULL) { 
    std::cout << "ERROR! new failed on line " << __LINE__ << "..." << std::endl; 
    return -1; 
} 
for (int i = 0; i < n; i++) { 
    array_ptr[i] = new double[m]; 
    if (array_ptr[i] == NULL) { 
     std::cout << "ERROR! new failed on line " << __LINE__ << "..." << std::endl; 
     return -1; 
    } 
} 

return 0; 
} 

或者,在C风格的时尚,我们可以使用另一个指针间接(使用&test呼叫代码通过double ** test的地址)。

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; 
} 

或者你可以简单地返回指向摆在首位的阵列 - 但这需要调用代码一些细微的变化:如果我有Malloc2D只有走

double** 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 NULL; 
} 
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 NULL; 
    } 
} 

return array_ptr; 
}