On Thursday 10 March 2005 14:20, Yoni wrote:
> On the same subject, can someone please explain a bit about the free()
> function ? what does it do, and how come you can only send it the memory
> address which you started allocating from ?

In ANSI C, you allocate a region of dynamically allocated memory using the 
malloc() function. This goes something like that:

<<<
char * ptr_to_buffer;
/* If I want a buffer with a size of 100,000 bytes */
ptr_to_buffer = malloc(100000); 
/* Do something with ptr_to_buffer */
.
.
.
/* Free ptr_to_buffer so we won't have any memory leaks. */
free(ptr_to_buffer);
>>>

The purpose of the free() function is to release the allocated memory region 
and allow the application to make a future use of it. You need to pass it the 
beginning of the allocated region because of the way the algorithm works. 
Namely, it designates a region before the pointer returned as the allocated 
memory, to keep track of various control parameters for the allocated memory 
buffer, and to know how to later free it.

Regards,

        Shlomi Fish


---------------------------------------------------------------------
Shlomi Fish      [EMAIL PROTECTED]
Homepage:        http://www.shlomifish.org/

Knuth is not God! It took him two days to build the Roman Empire.

--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://www.haifux.org)
To unsub send an empty message to [EMAIL PROTECTED]


Reply via email to