How Free() works?

V

vjay

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?
 
M

Mike Wahler

vjay said:
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
 
D

Derrick Coetzee

vjay said:
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*.
 
A

Allin Cottrell

Derrick said:
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
 
I

infobahn

vjay said:
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.
 
C

CBFalconer

Allin said:
.... 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.
 
A

Allin Cottrell

vjay said:
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
 
E

E. Robert Tisdale

vjay said:
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.
 
M

Malcolm

Allin Cottrell said:
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.
 
V

vjay

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.
 
R

Richard Bos

Malcolm said:
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
 
C

CBFalconer

Richard said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top