2016-06-11 2 views
2
私は私のコードでいくつかの問題を持っている

こんにちは皆:ダブルポインタの解放問題とポインタ

これは、行列の割り当て機能である:

matrix_type** matrix_allocation(MATRIX* matrix, int* rows_size, int* cols_size) 
//this function allocates dynamically a matrix and verify its allocation 
{ 
    int i; 
    matrix->n_rows=0; 
    matrix->n_cols=0; 
    matrix->pp_matrix=(matrix_type**)calloc(*rows_size,sizeof(matrix_type*)); 
    i=0; 
    while(i<*rows_size) 
    { 
     matrix->pp_matrix[i]=calloc(*cols_size,sizeof(matrix_type)); 
     i++; 
    } 
    matrix->n_rows=*rows_size; 
    matrix->n_cols=*cols_size; 
    return matrix->pp_matrix; 
} 

これは私の解放関数です:

void matrix_deallocation(MATRIX* matrix) 
//this function deallocates a matrix 
{ 
    int i; 
    i=0; 
    while(i<matrix->n_cols) 
    { 
     free(matrix->pp_matrix[i]); 
     i++; 
    } 
    free(matrix->pp_matrix); 
} 

MATRIX構造体が

は、どのように私はメインに解放関数を呼び出します。

matrix_deallocation(&table); 

この関数は、割り当てを解除半分だけ行列、なぜ?

Screen

+1

あなたは 'for'ループを聞いたことがあります意味らしい - –

+1

@Simoneジ・オールマイティーCicerelloループwhile''使用するよりも、コードをより読みやすくなりますが割り当てました行または列による行列? –

答えて

2

あなたが以下の

void matrix_deallocation(MATRIX* matrix) 
//this function deallocates a matrix 
{ 
    for (int i = 0; i < matrix->n_rows; i++) 
    //       ^^^^^^ 
    { 
     free(matrix->pp_matrix[i]); 
    } 
    free(matrix->pp_matrix); 
} 
+1

良い点ですが、スクリーンショットはそれほど有益ではありません。行と列の数は共に4です。 – Codor

+0

@Codor私はスクリーンショットを調査しませんでしたが、一見するとマトリックスには5つの列があるようです。 –

+0

あなたは正しいです。私の悪い。 – Codor