On Sun, 19 Nov 2006 19:22:29 +0700
Charles <[EMAIL PROTECTED]> wrote:

> Hello,
> 
> This is a question I have in mind for long but still haven't found the
> answer yet. I hope anyone could give me some clarity in this.
> 
> There are many library functions that returns dynamically allocated
> data. For example a function like this
> 
> char *createSomeData()
> {
> char *c = malloc(10000);
> return c;
> }
> 
> And then it is called somewhere...
> char *c = createSomeData();
> 
> The question is, when will the data be freed? Or should we free it
> ourselves, but I have never seen a C code that frees a pointer
> returned by some function. For example, there are many win32 api
> functions that takes a struct pointer as an argument. The struct must
> have been dynamically allocated in the api function, but I have never
> seen any instruction to free the struct returned when we no longer
> need the struct.

the dynamic memory is freed at program exit if a call to free(c) is not
made. this is ok, i suppose if the program is going to exit naturally
and you don't have this in a loop, but its not a good idea at all
really.

you could make a function like this:

void free_some_data( void *ptr ) {
        free(ptr);
}

if you really like. but i suppose the main reason for having a
create_some_data function is to setup a struct with some default values
i suppose.

-- 
Regards, Ed                      :: http://www.ednevitable.co.uk
just another unix person
Chuck Norris once had to bend Vin Diesel over his knee and spank him 
after he "disrespected" the roundhouse kick. 

Reply via email to