2013-07-02 57 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; 
}