主に C のスキルを磨くために、頻繁に使用される操作のための関数を備えた小さな C ライブラリを開発しています。解放関数のデバッグ セッションで、以前に 2 次元行列を指していたポインターの値がNULLではないことがわかりました。
割り当て解除後にポインターの状態を確認する方法がないことはわかっていますが (この質問を読んでから)、このポインターにNULL値を割り当てて、最終的にダングリング ポインターになることを回避しました。
機能コードは次のとおりです。
void matfree(void **M, int m){
int i;
for(i = 0; i < m; i ++){
free(M[i]);
M[i] = NULL;
}
free(M);
M = NULL;
}
テスト コード (別のファイルに):
int matfreetest(){
int **M = NULL;
M = (int **) matmalloc(0, 2, 2);
if(*M == NULL){
printf("Here 1! \n");
return 0;
}
matfree(M, 2);
if(*M == NULL){
return 1;
}
else{
printf("Here 2! \n");
return 0;
}
}
int main(){
int status;
status = matmalloctest();
printf("matmalloctest status = %d \n", status);
status = matfreetest();
printf("matfreetest status = %d \n", status);
system("PAUSE");
return 1;
}
matfreetest がステータス 1 を返すことを期待していましたが、0 を返しました。
編集: 要求に応じて、matmalloc のコード:
/* Memory allocator for any type and size of matrix.
Arguments:
- int type: identifier of the matrix' type
- int m: number of matrix rows
- int n: number of matrix columns
*/
void **matmalloc(int type, int m, int n){
int i;
void **M;
/* Check data type the do the proper allocation. */
if(type == INTEGER){ //int
M = (int **) malloc(m*sizeof(int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (int *) malloc(n*sizeof(int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == SINTEGER){ //short int
M = (short int **) malloc(m*sizeof(short int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (short int *) malloc(n*sizeof(short int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == LINTEGER){ //long int
M = (long int **) malloc(m*sizeof(long int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (long int *) malloc(n*sizeof(long int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == FL){ //float
M = (float **) malloc(m*sizeof(float *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (float *) malloc(n*sizeof(float));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == DOUB){ //double
M = (double **) malloc(m*sizeof(double *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (double *) malloc(n*sizeof(double));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == LDOUB){ //long double
M = (long double **) malloc(m*sizeof(long double *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (long double *) malloc(n*sizeof(long double));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else{
printf("Incorrect type of matrix data. Only numeric types are accepted. \n");
return NULL;
}
}