Allocated variable returning by a fuction is unallocated automatically ?

O

orion30

I would like to know, if an allocated variable returning by a fuction is
unallocated automatically ?

If no how to proceed ?

Best regards

------------------------------------------------------------------------

In my case, I want to return aa. After the call of polit, is aa is
unallocated automatically ?

char * polit(char *expressions)
{
..
..
..
aa = (char *)malloc(strlen(Result)*sizeof(char)+1);
..
..
..
return(aa);
}
 
B

Barry Schwarz

I would like to know, if an allocated variable returning by a fuction is
unallocated automatically ?

If no how to proceed ?

Best regards

------------------------------------------------------------------------

In my case, I want to return aa. After the call of polit, is aa is
unallocated automatically ?

char * polit(char *expressions)
{
.
.
.
aa = (char *)malloc(strlen(Result)*sizeof(char)+1);
.
.
.
return(aa);
}
If aa is an automatic variable defined in polit, then the variable
itself will go out of scope at the end of the function. The value
contained in the variable will be returned to the calling function
properly. Since this value is the address of a dynamically allocated
block of memory and since explicitly allocated memory must be
explicitly freed (which does not occur here), this value continues to
point to memory which remains available to the calling function.

To avoid memory leaks, the calling function should eventually free the
memory when it has served its purpose.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top