Home > Back-end >  Child function using malloc allocation of memory, how to release?
Child function using malloc allocation of memory, how to release?

Time:09-28

//child function
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;
}

//function
# include ", "
Void main () {

int a,b;
Double c;
A=3; B=3;
C=* mat (a, b);
Printf (" c=% f ", c);
getchar();
};

Small white consult everybody, the child function p point to release the memory? Release in a child function or in the calling routine release? What are the consequences if they don't release? If not release the end systems can be recycled?

CodePudding user response:

In theory the malloc to matching free

CodePudding user response:

External release so you now is not
You need a pointer variable to hold the return value
After finishing with free (pointer variable)

CodePudding user response:

As long as you keep the malloc return address, where the release of it doesn't matter, don't lose the address values make it ~
  • Related