471,066 Members | 937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Allocated variable returning by a fuction is unallocated automatically ?


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);
}
Nov 13 '05 #1
1 1919
On Mon, 14 Jul 2003 12:13:41 +0200, "orion30" <or*****@ifrance.com>
wrote:

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>>
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by bilbothebagginsbab5 AT freenet DOT de | last post: by
8 posts views Thread by ranjeet.gupta | last post: by
148 posts views Thread by onkar | last post: by
11 posts views Thread by mast2as | last post: by
7 posts views Thread by Serpent | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.