Link to home
Start Free TrialLog in
Avatar of netqueries
netqueries

asked on

How to free the memory allocated in the Local functions after the control returns to main program in C langugage

ex:
int main()
{
 fun();
}

void fun()
{
  char *str;
  int n;
  str = (char *) malloc(100);
  n = strlen(str);
  //some operations.
  return;
}
I wanted to know how & when to free the str.
will it be possible to free when the control goes back to main program.
Kindly help me.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PerryDK
PerryDK

In generall it is bad programming practice to call a function and to expect the caller of the function to free the memory associated with the function.

What you should do in 99.9% of circumstances is pass a pointer to the function such as

void fun(char* str)
{
  //caller of this function should guarentee that str is at least of size 100
  //do some operatons
  //modify str
}

int main()
{
  char* str = (char*)malloc(100);
  fun(str);
  //use str
  free(str);
}
Hi netqueries,
> str = (char *) malloc(100);
>   n = strlen(str);

Not good - what you get from malloc is not initialized.

BTW: If you know that the amount of memory is limited (it fits on the stack), you can use alloca() instead of malloc(). The memory will be freed automatically when the function returns.

Cheers,
Stefan
netqueries,

It's one of the problems of C that you don't have a "final" section which gets executed in every case. With your function it's simple, as you have only one return point:

oid fun()
{
  char *str;
  int n;
  str = (char *) malloc(100);
  n = strlen(str);
  //some operations.
  free(str);
  return;
}

But imagine a routine which has dozens of return points, some with implicit return (like via error checking macro, assert or exit). It gets messy quickly. Memory is returned to the system when the process ends, but not all ressources (think IPC!) are this grateful.

This is one of the few places where a goto can be appropriate *shudder*...

Stefan
Another way to do this is use the pointer as a global variable:
char *str;

Now u can free memory allocated to str anywhere u want.
Here,using the pointer as global doesnt take up too much memory as its only a pointer(size is 2 bytes or 4 for a far ptr)

int main()
{
fun();
//free(str); u can do this here
}

void fun()
{
 int n;
 str = (char *) malloc(100);
 n = strlen(str);
 //some operations.
//free(str); u can do this here
 return;
}
you can use atexit() or on_exit() to register function(s) which you wish to call upon exit

You will do fine with that unless you decide to call _exit instead of exit
Hi stefan,

Is alloca() part of the standard library or do u need a specific library?
I don't have it on my compiler(TC ver3.0)
ankuratvb,
alloca is defined in stdlib.h on my Solaris here.

Stefan
I guess the only reason(i think) the OP would want to free the mem. allocated in a local function when control is passed back to main() is if he wanted to modify,access the memory allocated or free it according to the value of some flag calc. by some processing logic.

Then ,u have 2 options:

1. Use the ptr as a global variable.

2. Declare the ptr in main() and pass the address to fun() to alloacte inside fun() and free either in fun() or in main().



Errm, correction: It's on the same man page as malloc, etc., but uses alloca.h
Stefan,
Looks like its time for me to get a new compiler :~)
i hope gcc will support it,
ankuratvb,
yes, it's even builtin:

#ifdef __GNUC__
#define alloca(size) __builtin_alloca(size)
#else
void * _EXFUN(alloca,(size_t));
#endif

Stefan
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wayside,
> create it on the stack as an array:
gcc also allows arrays of flexible size (probably using __builtin_alloca):

void arr_test(int asize){
    char buf[asize];
    ...
}

Stefan
> gcc also allows arrays of flexible size (probably using __builtin_alloca):

Nice. Microsoft's compiler doesn't allow this, although the runtime does have alloca().
wayside,
Yes, there are lots of really nice extensions. Some of them went into ISO99 (such as variable length arguments for macros), others are still unique. Especially the extended ASM is great.

Here is a list:
http://gcc.gnu.org/onlinedocs/gcc-3.3.3/gcc/C-Extensions.html#C%20Extensions

Stefan