//子函式
extern double *mat(int n, int m)
{
double *p;
if (!(p=(double *)malloc(sizeof(double)*n*m)))
{
fatalerr("matrix memory allocation error: n=%d,m=%d\n",n,m);
}
return p;
}
//主調函式
#include“。。。”
void main(){
int a,b;
double c;
a=3;b=3;
c= * mat(a,b);
printf("c=%f",c);
getchar();
};
小白請教各位大佬,這個子函式中p指向的記憶體怎么釋放?在子函式中釋放還是在主調函式中釋放?如果不釋放有什么后果?如果不釋放程式結束系統會回收么?
uj5u.com熱心網友回復:
理論上malloc要配對freeuj5u.com熱心網友回復:
外部釋放 所以你現在做法不行你得用一個指標變數保存回傳值
用完之后 free(指標變數)
uj5u.com熱心網友回復:
只要保存好malloc回傳的地址就好,在哪里釋放都沒關系。別把地址值搞丟了就行~轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/90282.html
標籤:C語言
