How Free() works?

  • Follow


Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?

0
Reply vijayanandham (9) 1/5/2005 2:07:21 AM

"vjay" <vijayanandham@gmail.com> wrote in message
news:6c848cc352c031589fe5d9b1cf2ef97d@localhost.talkaboutprogramming.com...
> Can someone help me understanding how free call works.how the free call
> will know that how many bytes has to free when i send pointer to that
> call?

Magic.

Seriously, the internal details of how 'free()'
works is an implementation-dependent issue, often
depending upon the workings of the host platform.
IOW it can easily be completely different among
implementations.  All you need to know is that
it 'just knows'.

http://www.eskimo.com/~scs/C-faq/top.html
See item 7.26

-Mike


0
Reply mkwahler (3821) 1/5/2005 2:50:41 AM


vjay wrote:
> Can someone help me understanding how free call works.how the free call
> will know that how many bytes has to free when i send pointer to that
> call?

 From an interface perspective, the important thing to remember is that 
you must pass free() a pointer returned by a previous 
malloc/calloc/realloc call, and the amount passed to that original call 
indicates how many bytes it frees. You can't, for example, allocate an 
array and then free an element of it; free(a[5]) would attempt to free 
the space the pointer in a[5] *points to*.

-- 
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
0
Reply dcnews (126) 1/5/2005 5:08:58 AM

Derrick Coetzee wrote:
> vjay wrote:
> 
>> Can someone help me understanding how free call works.how the free call
>> will know that how many bytes has to free when i send pointer to that
>> call?
> 
>  From an interface perspective, the important thing to remember is that 
> you must pass free() a pointer returned by a previous 
> malloc/calloc/realloc call...

True enough.  From a comp.lang.c point of view the primary response is
that the internal implementation of free() is system-specific and not
on-topic here.  The C standard specifies that free() must behave in
such-and-such a way, but says nothing about how that is to be
achieved.  How to implement free() is an interesting programming topic
in its own right, but not specifically a C question.

(Note: but it is possible to implement the malloc()/free() couple
in standard C, and you can read about that in K&R2, the second edition
of Kernighan and Ritchie's "The C Programming Language").

Alin Cottrell
0
Reply cottrell (169) 1/5/2005 5:32:32 AM

vjay wrote:
> 
> Can someone help me understanding how free call works.how the free call
> will know that how many bytes has to free when i send pointer to that
> call?

It depends.

It is reasonable to surmise that the memory allocation subsystem
keeps track of this information internally. As an exercise, write
a simple memory allocation subsystem yourself, with the interface:

  void *my_malloc(size_t);
  void my_free(void *);

Doing this will help you to understand.
0
Reply infobahn (503) 1/5/2005 5:49:14 AM

Allin Cottrell wrote:
> 
.... snip ...
> 
> (Note: but it is possible to implement the malloc()/free() couple
> in standard C, and you can read about that in K&R2, the second
> edition of Kernighan and Ritchie's "The C Programming Language").

No it isn't.  It is necessary to make assumptions about at least
alignment.  Thus malloc and friends must be system specific, and
supplied as part of the implementation.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

0
Reply cbfalconer (19183) 1/5/2005 11:59:34 AM

vjay wrote:
> Can someone help me understanding how free call works.how the free call
> will know that how many bytes has to free when i send pointer to that
> call?

Probably not anyone in comp.lang.c, where we talk about standard,
platform-independent C.  The details of how free() works live at a
different level, that of the specific implementation.

You'd do better looking up references to the C library on the
platform you're most interested in.

Allin Cottrell
0
Reply cottrell (169) 1/7/2005 2:06:23 AM

vjay wrote:

> Can someone help me understanding how [the] free call works?
> How [does] free know how many bytes [must be] free'd
> when I [pass] a pointer to free?

In the typical implementation,
the size of the allocated memory is stored in a free list.

I used Google

	http://www.google.com/

to search for

	+"free list" +"malloc" +"free"

and I found lots of stuff including:

http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Malloc2/lecture.html

Please tell us which compiler and operating system you are using
and we may be able to direct you to a forum where there are experts
on your particular implementation.
0
Reply E.Robert.Tisdale (2031) 1/7/2005 2:29:52 AM

"Allin Cottrell" <cottrell@wfu.edu> wrote
> vjay wrote:
> > Can someone help me understanding how free call works.how the free
> > call will know that how many bytes has to free when i send pointer to
> > that call?
>
> Probably not anyone in comp.lang.c, where we talk about standard,
> platform-independent C.  The details of how free() works live at a
> different level, that of the specific implementation.
>
Some insight into how to implement a standard library function is I think
topical, though of course we cannot speak for every compiler.

The trick is to store internal information in the block of memory
immediately before the pointer returned by malloc(). This will include the
size of the block allocated, and pointers to the next and previous blocks.
free() then uses this information to mark the block as freed, and if
necessary to merge it with adjacent free blocks ready for the next
allocation.


0
Reply Malcolm 1/9/2005 12:04:15 AM

Thank you very much guys.The link by robert was useful.I shouldn't have
asked the question since the topic is so general but comp.lang.c is the
one i visit frequently. 

0
Reply vijayanandham (9) 1/9/2005 4:02:43 PM

"Malcolm" <malcolm@55bank.freeserve.co.uk> wrote:

> "Allin Cottrell" <cottrell@wfu.edu> wrote
> > vjay wrote:
> > > Can someone help me understanding how free call works.how the free
> > > call will know that how many bytes has to free when i send pointer to
> > > that call?
> >
> > Probably not anyone in comp.lang.c, where we talk about standard,
> > platform-independent C.  The details of how free() works live at a
> > different level, that of the specific implementation.
>
> Some insight into how to implement a standard library function is I think
> topical, though of course we cannot speak for every compiler.

The problem is that there's rarely _the_ way to implement a Standard
function. There are usually several ways. For example...

> The trick is to store internal information in the block of memory
> immediately before the pointer returned by malloc().

....this is simply wrong. That is _one_ possible way to implement a
malloc() package, and a popular one; but it is by no means the only way,
or even the only good way, and to suggest that this is "the trick" is
misleading.

Richard
0
Reply rlb (4118) 1/10/2005 8:58:21 AM

Richard Bos wrote:
> "Malcolm" <malcolm@55bank.freeserve.co.uk> wrote:
>> "Allin Cottrell" <cottrell@wfu.edu> wrote
>>> vjay wrote:
> 
>>>> Can someone help me understanding how free call works.how the
>>>> free call will know that how many bytes has to free when i send
>>>> pointer to that call?
>>>
>>> Probably not anyone in comp.lang.c, where we talk about standard,
>>> platform-independent C.  The details of how free() works live at
>>> a different level, that of the specific implementation.
>>
>> Some insight into how to implement a standard library function is
>> I think topical, though of course we cannot speak for every
>> compiler.
> 
> The problem is that there's rarely _the_ way to implement a
> Standard function. There are usually several ways. For example...

You can find one example, system specific to DJGPP, on my site
below, download section (nmalloc.zip).  It will probably also
function on most Unices, but is definitely not portable according
to the standards we use here.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


0
Reply cbfalconer (19183) 1/10/2005 2:03:38 PM

11 Replies
51 Views

(page loaded in 0.183 seconds)


Reply: